Merge new filemanager
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
|
||||
import superagent from 'superagent';
|
||||
import safe from 'safetydance';
|
||||
import { sanitize } from 'pankow/utils';
|
||||
|
||||
export function createDirectoryModel(origin, accessToken, api) {
|
||||
|
||||
return {
|
||||
name: 'DirectoryModel',
|
||||
async listFiles(path) {
|
||||
const [error, result] = await safe(superagent.get(`${origin}/api/v1/${api}/files/${path}`).query({ access_token: accessToken }));
|
||||
if (error) {
|
||||
console.error('Failed to list files', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
// this prepares the entries to be compatible with all components
|
||||
result.body.entries.forEach(item => {
|
||||
// if we have an image, attach previewUrl
|
||||
if (item.mimeType.indexOf('image/') === 0) {
|
||||
item.previewUrl = `${origin}/api/v1/${api}/files/${encodeURIComponent(path + '/' + item.fileName)}?access_token=${accessToken}`
|
||||
}
|
||||
|
||||
item.folderPath = path;
|
||||
});
|
||||
|
||||
return result.body.entries;
|
||||
},
|
||||
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);
|
||||
},
|
||||
async newFile(folderPath, fileName) {
|
||||
await superagent.post(`${origin}/api/v1/${api}/files/${folderPath}`)
|
||||
.query({ access_token: accessToken })
|
||||
.attach('file', new File([], fileName));
|
||||
},
|
||||
async newFolder(folderPath) {
|
||||
await superagent.post(`${origin}/api/v1/${api}/files/${folderPath}`)
|
||||
.query({ access_token: accessToken })
|
||||
.send({ directory: true });
|
||||
},
|
||||
async remove(filePath) {
|
||||
await superagent.del(`${origin}/api/v1/${api}/files/${filePath}`)
|
||||
.query({ access_token: accessToken });
|
||||
},
|
||||
async rename(fromFilePath, toFilePath) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
|
||||
.send({ action: 'rename', newFilePath: sanitize(toFilePath) })
|
||||
.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');
|
||||
},
|
||||
async getFile(path) {
|
||||
const [error, result] = await safe(fetch(`${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`));
|
||||
if (error) {
|
||||
console.error('Failed to get file', error);
|
||||
return null;
|
||||
}
|
||||
|
||||
const text = await result.text();
|
||||
return text;
|
||||
},
|
||||
getFileUrl(path) {
|
||||
return `${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
createDirectoryModel
|
||||
};
|
||||
Reference in New Issue
Block a user