You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.2 KiB
110 lines
2.2 KiB
const {query, queryForm} = require("../client")
|
|
|
|
class File {
|
|
/**
|
|
* @param {{id:string,kind:string,time:Date|string|number,public:boolean,name:string,mimeType:string,size:number,author:string,url:string}} data
|
|
*/
|
|
constructor(data) {
|
|
this.id = data.id
|
|
this.kind = data.kind
|
|
this.time = new Date(data.time)
|
|
this.public = data.public
|
|
this.name = data.name
|
|
this.mimeType = data.mimeType
|
|
this.size = data.size
|
|
this.author = data.author
|
|
this.url = data.url
|
|
}
|
|
}
|
|
|
|
|
|
class FileAPI {
|
|
/**
|
|
* Call `characters(filter)` query
|
|
*
|
|
* @param {{public:boolean,mimeTypes:string[]}} filter
|
|
* @returns {Promise<File[]>}
|
|
*/
|
|
list(filter = {public: true}, opts = {}) {
|
|
return query(`
|
|
query ListFiles($filter: FilesFilter!) {
|
|
files(filter:$filter) {
|
|
id
|
|
kind
|
|
time
|
|
public
|
|
name
|
|
mimeType
|
|
size
|
|
author
|
|
url
|
|
}
|
|
}
|
|
`, {filter}, opts).then(({files}) => {
|
|
return files.map(d => new File(d))
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Upload a file
|
|
*
|
|
* @param {{public:boolean, file:File, name:string}} input
|
|
*/
|
|
uploadFile(input) {
|
|
return queryForm(`
|
|
mutation UploadFile($input: FileUploadInput!) {
|
|
uploadFile(input:$input) {
|
|
id
|
|
kind
|
|
time
|
|
public
|
|
name
|
|
mimeType
|
|
size
|
|
author
|
|
url
|
|
}
|
|
}
|
|
`, {input}).then(({uploadFile}) => {
|
|
return new File(uploadFile)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Edit a file
|
|
*
|
|
* @param {{id: string, public:boolean, name:string}} input
|
|
*/
|
|
editFile(input) {
|
|
return queryForm(`
|
|
mutation EditFile($input: FileEditInput!) {
|
|
editFile(input:$input) {
|
|
id
|
|
public
|
|
name
|
|
}
|
|
}
|
|
`, {input}).then(({editFile}) => {
|
|
return editFile
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Edit a file
|
|
*
|
|
* @param {{id: string, public:boolean, name:string}} input
|
|
*/
|
|
removeFile(input) {
|
|
return queryForm(`
|
|
mutation RemoveFIle($input: FileRemoveInput!) {
|
|
removeFile(input:$input) {
|
|
id
|
|
}
|
|
}
|
|
`, {input}).then(({removeFile}) => {
|
|
return removeFile
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = { File, fileApi: new FileAPI() }
|