2023-02-26 23:34:31 +01:00
|
|
|
|
|
|
|
|
import superagent from 'superagent';
|
|
|
|
|
import safe from 'safetydance';
|
2023-04-02 10:06:14 +02:00
|
|
|
import { sanitize } from 'pankow/utils';
|
2023-02-26 23:34:31 +01:00
|
|
|
|
2023-04-11 12:14:47 +02:00
|
|
|
export function createDirectoryModel(origin, accessToken, api) {
|
|
|
|
|
|
2023-02-26 23:34:31 +01:00
|
|
|
return {
|
|
|
|
|
name: 'DirectoryModel',
|
|
|
|
|
async listFiles(path) {
|
2023-04-11 12:14:47 +02:00
|
|
|
const [error, result] = await safe(superagent.get(`${origin}/api/v1/${api}/files/${path}`).query({ access_token: accessToken }));
|
2023-02-26 23:34:31 +01:00
|
|
|
if (error) {
|
|
|
|
|
console.error('Failed to list files', error);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 10:41:02 +02:00
|
|
|
// this prepares the entries to be compatible with all components
|
2023-03-29 20:02:26 +02:00
|
|
|
result.body.entries.forEach(item => {
|
2023-04-02 10:41:02 +02:00
|
|
|
// if we have an image, attach previewUrl
|
2023-03-29 20:02:26 +02:00
|
|
|
if (item.mimeType.indexOf('image/') === 0) {
|
2023-04-11 12:14:47 +02:00
|
|
|
item.previewUrl = `${origin}/api/v1/${api}/files/${encodeURIComponent(path + '/' + item.fileName)}?access_token=${accessToken}`
|
2023-03-29 20:02:26 +02:00
|
|
|
}
|
2023-04-02 10:41:02 +02:00
|
|
|
|
|
|
|
|
item.folderPath = path;
|
2023-03-29 20:02:26 +02:00
|
|
|
});
|
|
|
|
|
|
2023-02-26 23:34:31 +01:00
|
|
|
return result.body.entries;
|
|
|
|
|
},
|
2023-04-11 16:29:58 +02:00
|
|
|
async upload(targetDir, file, progressHandler) {
|
|
|
|
|
await superagent.post(`${origin}/api/v1/${api}/files/${encodeURIComponent(sanitize(targetDir + '/' + file.name))}`)
|
|
|
|
|
.query({ access_token: accessToken })
|
|
|
|
|
.attach('file', file)
|
|
|
|
|
.on('progress', progressHandler);
|
|
|
|
|
},
|
2023-04-02 10:06:14 +02:00
|
|
|
async remove(filePath) {
|
2023-04-12 15:59:48 +02:00
|
|
|
await superagent.del(`${origin}/api/v1/${api}/files/${filePath}`)
|
|
|
|
|
.query({ access_token: accessToken });
|
2023-04-02 10:06:14 +02:00
|
|
|
},
|
|
|
|
|
async rename(fromFilePath, toFilePath) {
|
2023-04-12 15:59:48 +02:00
|
|
|
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
|
2023-04-02 10:06:14 +02:00
|
|
|
.send({ action: 'rename', newFilePath: sanitize(toFilePath) })
|
2023-04-12 15:59:48 +02:00
|
|
|
.query({ access_token: accessToken });
|
|
|
|
|
},
|
|
|
|
|
async save(filePath, content) {
|
|
|
|
|
const file = new File([content], 'file');
|
|
|
|
|
await superagent.post(`${origin}/api/v1/${api}/files/${filePath}`)
|
|
|
|
|
.query({ access_token: accessToken })
|
|
|
|
|
.attach('file', file)
|
|
|
|
|
.field('overwrite', 'true');
|
2023-02-26 23:34:31 +01:00
|
|
|
},
|
|
|
|
|
async getFile(path) {
|
2023-04-11 12:14:47 +02:00
|
|
|
const [error, result] = await safe(fetch(`${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`));
|
2023-02-26 23:34:31 +01:00
|
|
|
if (error) {
|
|
|
|
|
console.error('Failed to get file', error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 00:10:55 +01:00
|
|
|
const text = await result.text();
|
|
|
|
|
return text;
|
2023-04-01 11:33:22 +02:00
|
|
|
},
|
|
|
|
|
getFileUrl(path) {
|
2023-04-11 12:14:47 +02:00
|
|
|
return `${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`;
|
2023-02-26 23:34:31 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
createDirectoryModel
|
|
|
|
|
};
|