~thrrgilag/woodstock

woodstock/files.go -rw-r--r-- 3.8 KiB
62b10b5cMorgan McMillian Update for new project location and module name 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package woodstock

import (
	"encoding/json"
	"io"
	"net/url"
	"strings"
)

// FileResult response definition
type FileResult struct {
	*CommonResponse
	Data File `json:"data"`
}

// FilesResult response definition
type FilesResult struct {
	*CommonResponse
	Data []File `json:"data"`
}

// FileDetails object definition
type FileDetails struct {
	Name     string `json:"name"`
	IsPublic bool   `json:"is_public"`
}

// GetFile retrieve a file object
// https://pnut.io/docs/resources/files/lookup#get-files-id
func (c *Client) GetFile(id string) (result FileResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetFiles retrieve multiple file objects
// https://pnut.io/docs/resources/files/lookup#get-files
func (c *Client) GetFiles(ids []string) (result FilesResult, err error) {
	v := url.Values{}
	v.Set("ids", strings.Join(ids, ","))
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI + "?" + v.Encode(), data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetUserFiles retrieve the authenticated users file objects
// https://pnut.io/docs/resources/files/lookup#get-users-me-files
func (c *Client) GetUserFiles() (result FilesResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/files", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// CreateFile upload a complete file
// https://pnut.io/docs/resources/files/lifecycle#post-files
func (c *Client) CreateFile(params map[string]string, reader io.Reader) (result FileResult, err error) {
	// v.Set("field", "content")
	params["field"] = "content"
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI, params: params, reader: reader, data: &result, method: "POST", responseCh: responseCh}
	return result, (<-responseCh).err
}

// CreateFilePlaceholder create a file placeholder
// https://pnut.io/docs/resources/files/lifecycle#post-files
func (c *Client) CreateFilePlaceholder(params File) (result FileResult, err error) {
	json, err := json.Marshal(params)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI, data: &result, method: "POST", json: string(json), responseCh: responseCh}
	return result, (<-responseCh).err
}

// TODO: is broken
// UpdateFileContent uploads a file for an existing file placeholder
// https://pnut.io/docs/resources/files/lifecycle#put-files-id-content
// func (c *Client) UpdateFileContent(id string, reader io.Reader) (result FileResult, err error) {
// 	responseCh := make(chan response)
// 	c.queryQueue <- query{url: FileAPI + "/" + id + "/content", reader: reader, data: &result, method: "PUT", responseCh: responseCh}
// 	return result, (<-responseCh).err
// }

// UpdateFileDetails updates a files details, only name, is_public, and raw
// https://pnut.io/docs/resources/files/lifecycle#put-files-id
func (c *Client) UpdateFileDetails(id string, details FileDetails) (result FileResult, err error) {
	json, err := json.Marshal(details)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI + "/" + id, data: &result, method: "PUT", json: string(json), responseCh: responseCh}
	return result, (<-responseCh).err
}

// DeleteFile delete a file. This will not disassociate a file with any other objects (posts, messages...)
// https://pnut.io/docs/resources/files/lifecycle#delete-files-id
func (c *Client) DeleteFile(id string) (result FileResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: FileAPI + "/" + id, data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}