github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/convert/apidocumizecom/msword.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  package apidocumizecom
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"fmt"
    18  	"net/http"
    19  	"path/filepath"
    20  
    21  	api "github.com/documize/community/core/convapi"
    22  
    23  	"golang.org/x/net/context"
    24  )
    25  
    26  // Msword type provides a peg to hang the Convert method on.
    27  type Msword struct{}
    28  
    29  // Convert converts a file into the Countersoft Documize format.
    30  func (file *Msword) Convert(r api.DocumentConversionRequest, reply *api.DocumentConversionResponse) error {
    31  	byts, err := json.Marshal(r)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	base := filepath.Base(r.Filename)
    36  	fmt.Println("Starting conversion of document: ", base)
    37  
    38  	client := &http.Client{Transport: transport}
    39  
    40  	tok, err := token()
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	resp, err := client.Post(endPoint()+"/api/word?token="+tok, "application/json", bytes.NewReader(byts))
    46  	if err != nil {
    47  		return err
    48  	}
    49  	defer func() {
    50  		if e := resp.Body.Close(); e != nil {
    51  			fmt.Println("resp.Body.Close error: " + e.Error())
    52  		}
    53  	}()
    54  
    55  	fmt.Println("Finished converting document: ", base)
    56  
    57  	dec := json.NewDecoder(resp.Body)
    58  	err = dec.Decode(reply)
    59  
    60  	return err
    61  }
    62  
    63  // MSwordConvert provides the standard interface for conversion of a MS-Word document.
    64  // All the function does is return a pointer to api.DocumentConversionResponse with
    65  // PagesHTML set to the given (*api.DocumentConversionRequest).Filedata converted by the Documize server.
    66  func MSwordConvert(ctx context.Context, in interface{}) (interface{}, error) {
    67  	var msw Msword
    68  	dcr := in.(*api.DocumentConversionRequest)
    69  	rep := new(api.DocumentConversionResponse)
    70  	err := msw.Convert(*dcr, rep)
    71  	return rep, err
    72  }