Files
cloudron-box/frontend/src/models/DirectoryModel.js

143 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-02-26 23:34:31 +01:00
import superagent from 'superagent';
import { buildFilePath, sanitize } from 'pankow/utils';
2023-02-26 23:34:31 +01:00
2023-04-17 17:07:25 +02:00
const BASE_URL = import.meta.env.BASE_URL || '/';
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-16 19:06:14 +02:00
let error, result;
try {
result = await superagent.get(`${origin}/api/v1/${api}/files/${path}`).query({ access_token: accessToken });
} catch (e) {
error = e;
}
if (error || result.statusCode !== 200) {
if (error.status === 404) return [];
2023-04-16 19:06:14 +02:00
console.error('Failed to list files', error || result.statusCode);
2023-02-26 23:34:31 +01:00
return [];
}
2023-04-02 10:41:02 +02:00
// this prepares the entries to be compatible with all components
result.body.entries.forEach(item => {
2023-04-17 17:07:25 +02:00
item.id = item.fileName;
item.name = item.fileName;
item.folderPath = path;
item.modified = new Date(item.mtime);
item.type = item.isDirectory ? 'directory' : 'file',
item.icon = `${BASE_URL}mime-types/${item.mimeType === 'inode/symlink' ? 'none' : item.mimeType.split('/').join('-')}.svg`;
2023-04-02 10:41:02 +02:00
// if we have an image, attach previewUrl
if (item.mimeType.indexOf('image/') === 0) {
2023-04-16 19:06:14 +02:00
item.previewUrl = `${origin}/api/v1/${api}/files/${encodeURIComponent(path + '/' + item.fileName)}?access_token=${accessToken}`;
}
2023-04-02 10:41:02 +02:00
2023-04-17 17:07:25 +02:00
item.owner = 'unkown';
if (item.uid === 0) item.owner = 'root';
if (item.uid === 33) item.owner = 'www-data';
if (item.uid === 1000) item.owner = 'cloudron';
if (item.uid === 1001) item.owner = 'git';
});
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) {
// file may contain a file name or a file path + file name
const relativefilePath = (file.webkitRelativePath ? file.webkitRelativePath : file.name);
await superagent.post(`${origin}/api/v1/${api}/files/${encodeURIComponent(sanitize(targetDir + '/' + relativefilePath))}`)
2023-04-11 16:29:58 +02:00
.query({ access_token: accessToken })
.attach('file', file)
.on('progress', progressHandler);
},
2023-04-16 13:43:48 +02:00
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 });
},
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 copy(fromFilePath, toFilePath) {
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
.send({ action: 'copy', newFilePath: sanitize(toFilePath) })
.query({ access_token: accessToken });
},
2023-04-17 17:51:19 +02:00
async chown(filePath, uid) {
await superagent.put(`${origin}/api/v1/${api}/files/${filePath}`)
.send({ action: 'chown', uid: uid, recursive: true })
.query({ access_token: accessToken });
},
async extract(path) {
await superagent.put(`${origin}/api/v1/${api}/files/${path}`)
.send({ action: 'extract' })
.query({ access_token: accessToken });
},
async download(path) {
window.open(`${origin}/api/v1/${api}/files/${path}?download=true&access_token=${accessToken}`);
},
2023-04-12 15:59:48 +02:00
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-16 19:06:14 +02:00
let result;
try {
result = await fetch(`${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`);
} catch (error) {
2023-02-26 23:34:31 +01:00
console.error('Failed to get file', error);
return null;
}
const text = await result.text();
return text;
2023-04-01 11:33:22 +02:00
},
async paste(targetDir, action, files) {
// this will not overwrite but tries to find a new unique name to past to
for (let f in files) {
let done = false;
let targetPath = targetDir + '/' + files[f].name;
while (!done) {
try {
if (action === 'cut') await this.rename(buildFilePath(files[f].folderPath, files[f].name), targetPath);
if (action === 'copy') await this.copy(buildFilePath(files[f].folderPath, files[f].name), targetPath);
done = true;
} catch (error) {
if (error.status === 409) {
targetPath += '-copy';
} else {
throw error;
}
}
}
}
},
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
};