github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/sdk/attachment.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 documize
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"errors"
    18  	"fmt"
    19  	"io/ioutil"
    20  	"mime/multipart"
    21  	"net/http"
    22  
    23  	"github.com/documize/community/documize/api/entity"
    24  )
    25  
    26  const emptyBraces = "{}"
    27  
    28  // GetAttachmentData get the data of a file attachement.
    29  func (c *Client) GetAttachmentData(att *entity.Attachment) error {
    30  	url := fmt.Sprintf("%s/api/public/attachments/%s/%s/%s",
    31  		c.BaseURL, att.OrgID, att.Job, att.FileID)
    32  	req, err := http.NewRequest("GET", url, nil)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
    37  	resp, err := c.Client.Do(req)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	defer resp.Body.Close() // ignore error
    42  	b, err := ioutil.ReadAll(resp.Body)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	if isError(string(b)) {
    47  		return errors.New(trimErrors(string(b)))
    48  	}
    49  	att.Data = b
    50  	return nil
    51  }
    52  
    53  // DeleteAttachment removes a file attachment.
    54  func (c *Client) DeleteAttachment(att *entity.Attachment) error {
    55  	url := fmt.Sprintf("%s/api/documents/%s/attachments/%s",
    56  		c.BaseURL, att.DocumentID, att.RefID)
    57  	req, err := http.NewRequest("DELETE", url, nil)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
    62  	resp, err := c.Client.Do(req)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	defer resp.Body.Close() // ignore error
    67  	b, err := ioutil.ReadAll(resp.Body)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	if string(b) != emptyBraces {
    72  		return errors.New(trimErrors(string(b)))
    73  	}
    74  	return nil
    75  }
    76  
    77  // GetAttachments gets a slice of the attachments for a document ID.
    78  func (c *Client) GetAttachments(documentID string) (entAtts []entity.Attachment, err error) {
    79  	url := fmt.Sprintf("%s/api/documents/%s/attachments",
    80  		c.BaseURL, documentID)
    81  	req, err := http.NewRequest("GET", url, nil)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
    86  	resp, err := c.Client.Do(req)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	defer resp.Body.Close() // ignore error
    91  	b, err := ioutil.ReadAll(resp.Body)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	err = json.Unmarshal(b, &entAtts)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	return entAtts, nil
   100  }
   101  
   102  // AddAttachment adds a new attachement to a document
   103  func (c *Client) AddAttachment(documentID, filename string, data []byte) error {
   104  	url := fmt.Sprintf("%s/api/documents/%s/attachments",
   105  		c.BaseURL, documentID)
   106  	var buf bytes.Buffer
   107  	w := multipart.NewWriter(&buf)
   108  
   109  	writer, err := w.CreateFormFile("attachment", filename)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	n, err := writer.Write(data)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	if n != len(data) {
   118  		return errors.New("incorrect length written")
   119  	}
   120  	err = w.Close()
   121  	if err != nil {
   122  		return err
   123  	}
   124  	req, err := http.NewRequest("POST", url, bytes.NewReader(buf.Bytes()))
   125  	if err != nil {
   126  		return err
   127  	}
   128  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
   129  	req.Header.Set("Content-Type",
   130  		"multipart/form-data; boundary="+w.Boundary())
   131  	resp, err := c.Client.Do(req)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	defer resp.Body.Close() // ignore error
   136  	b, err := ioutil.ReadAll(resp.Body)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	if string(b) != emptyBraces {
   141  		return errors.New(trimErrors(string(b)))
   142  	}
   143  	return nil
   144  }