2020-01-29 03:22:13 +13:00
|
|
|
package appwrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Storage service
|
|
|
|
type Storage struct {
|
2020-01-29 09:24:42 +13:00
|
|
|
client Client
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:08:39 +13:00
|
|
|
func NewStorage(clt Client) Storage {
|
2020-01-29 12:14:30 +13:00
|
|
|
service := Storage{
|
|
|
|
client: clt,
|
|
|
|
}
|
|
|
|
|
2020-01-29 10:01:07 +13:00
|
|
|
return service
|
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// ListFiles get a list of all the user files. You can use the query params to
|
2020-01-29 03:22:13 +13:00
|
|
|
// filter your results. On admin mode, this endpoint will return a list of all
|
|
|
|
// of the project files. [Learn more about different API modes](/docs/admin).
|
2020-01-31 09:58:49 +13:00
|
|
|
func (srv *Storage) ListFiles(Search string, Limit int, Offset int, OrderType string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
path := "/storage/files"
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"search": Search,
|
|
|
|
"limit": Limit,
|
|
|
|
"offset": Offset,
|
|
|
|
"orderType": OrderType,
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("GET", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// CreateFile create a new file. The user who creates the file will
|
|
|
|
// automatically be assigned to read and write access unless he has passed
|
|
|
|
// custom values for read and write arguments.
|
|
|
|
func (srv *Storage) CreateFile(File string, Read []interface{}, Write []interface{}) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
path := "/storage/files"
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"file": File,
|
|
|
|
"read": Read,
|
|
|
|
"write": Write,
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("POST", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// GetFile get file by its unique ID. This endpoint response returns a JSON
|
|
|
|
// object with the file metadata.
|
|
|
|
func (srv *Storage) GetFile(FileId string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("GET", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// UpdateFile update file by its unique ID. Only users with write permissions
|
|
|
|
// have access to update this resource.
|
|
|
|
func (srv *Storage) UpdateFile(FileId string, Read []interface{}, Write []interface{}) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"read": Read,
|
|
|
|
"write": Write,
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("PUT", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// DeleteFile delete a file by its unique ID. Only users with write
|
|
|
|
// permissions have access to delete this resource.
|
|
|
|
func (srv *Storage) DeleteFile(FileId string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("DELETE", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// GetFileDownload get file content by its unique ID. The endpoint response
|
|
|
|
// return with a 'Content-Disposition: attachment' header that tells the
|
|
|
|
// browser to start downloading the file to user downloads directory.
|
|
|
|
func (srv *Storage) GetFileDownload(FileId string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}/download")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("GET", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// GetFilePreview get a file preview image. Currently, this method supports
|
2020-01-29 03:22:13 +13:00
|
|
|
// preview for image files (jpg, png, and gif), other supported formats, like
|
|
|
|
// pdf, docs, slides, and spreadsheets, will return the file icon image. You
|
|
|
|
// can also pass query string arguments for cutting and resizing your preview
|
|
|
|
// image.
|
2020-01-31 09:58:49 +13:00
|
|
|
func (srv *Storage) GetFilePreview(FileId string, Width int, Height int, Quality int, Background string, Output string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}/preview")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"width": Width,
|
|
|
|
"height": Height,
|
|
|
|
"quality": Quality,
|
|
|
|
"background": Background,
|
|
|
|
"output": Output,
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("GET", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:58:49 +13:00
|
|
|
// GetFileView get file content by its unique ID. This endpoint is similar to
|
|
|
|
// the download method but returns with no 'Content-Disposition: attachment'
|
2020-01-29 03:22:13 +13:00
|
|
|
// header.
|
2020-01-31 09:58:49 +13:00
|
|
|
func (srv *Storage) GetFileView(FileId string, As string) (map[string]interface{}, error) {
|
2020-01-29 03:22:13 +13:00
|
|
|
r := strings.NewReplacer("{fileId}", FileId)
|
|
|
|
path := r.Replace("/storage/files/{fileId}/view")
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"as": As,
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:56:24 +13:00
|
|
|
return srv.client.Call("GET", path, nil, params)
|
2020-01-29 03:22:13 +13:00
|
|
|
}
|