Files
cloudron-box/dashboard/src/models/DirectoryModel.js
T

208 lines
6.8 KiB
JavaScript
Raw Normal View History

2023-02-26 23:34:31 +01:00
import { fetcher } from 'pankow';
2023-09-20 10:04:24 +02:00
import { 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-11-27 13:09:42 +01:00
export function createDirectoryModel(origin, accessToken, api) {
const ownersModel = [{
uid: 0,
label: 'root'
}, {
uid: 33,
label: 'www-data'
}, {
uid: 808,
label: 'yellowtent'
}, {
uid: 1000,
label: 'cloudron'
}, {
uid: 1001,
label: 'git'
}];
2023-04-11 12:14:47 +02:00
2023-02-26 23:34:31 +01:00
return {
name: 'DirectoryModel',
2023-11-27 13:09:42 +01:00
ownersModel,
2023-09-20 10:04:24 +02:00
buildFilePath(filePath, fileName) {
// remove leading and trailing slashes
while (filePath.startsWith('/')) filePath = filePath.slice(1);
while (filePath.endsWith('/')) filePath = filePath.slice(0, -1);
return encodeURIComponent(`${filePath}${filePath ? '/' : ''}${fileName}`);
},
2023-02-26 23:34:31 +01:00
async listFiles(path) {
2023-04-16 19:06:14 +02:00
let error, result;
try {
result = await fetcher.get(`${origin}/api/v1/${api}/files/${path}`, { access_token: accessToken });
2023-04-16 19:06:14 +02:00
} catch (e) {
error = e;
}
if (error || result.status !== 200) {
2023-04-25 22:45:22 +02:00
if (error.status === 404) return [];
console.error('Failed to list files', error || result.status);
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
2023-03-29 20:02:26 +02:00
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
2023-03-29 20:02:26 +02:00
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}`;
} else {
item.previewUrl = '';
2023-03-29 20:02:26 +02:00
}
2023-04-02 10:41:02 +02:00
item.owner = item.uid;
if (ownersModel.find((m) => m.uid === item.uid)) item.owner = ownersModel.find((m) => m.uid === item.uid).label;
2023-03-29 20:02:26 +02:00
});
2023-02-26 23:34:31 +01:00
return result.body.entries;
},
2024-08-19 17:09:04 +02:00
upload(targetDir, file, progressHandler) {
2023-04-29 16:10:34 +02:00
// file may contain a file name or a file path + file name
const relativefilePath = (file.webkitRelativePath ? file.webkitRelativePath : file.name);
2024-08-19 17:09:04 +02:00
const xhr = new XMLHttpRequest();
const req = new Promise(function (resolve, reject) {
xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
} else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
});
xhr.addEventListener('error', () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
});
xhr.upload.addEventListener('progress', (event) => {
if (event.loaded) progressHandler({ direction: 'upload', loaded: event.loaded});
});
xhr.open('POST', `${origin}/api/v1/${api}/files/${encodeURIComponent(sanitize(targetDir + '/' + relativefilePath))}?access_token=${accessToken}&overwrite=true`);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(file);
});
2024-08-19 17:09:04 +02:00
// attach for upstream xhr.abort()
req.xhr = xhr;
return req;
2023-04-11 16:29:58 +02:00
},
async newFile(filePath) {
2024-08-19 17:09:04 +02:00
await this.save(filePath, '');
2023-04-16 13:43:48 +02:00
},
async newFolder(folderPath) {
await fetcher.post(`${origin}/api/v1/${api}/files/${folderPath}`, {}, { directory: true, access_token: accessToken });
2023-04-16 13:43:48 +02:00
},
2023-04-02 10:06:14 +02:00
async remove(filePath) {
await fetcher.del(`${origin}/api/v1/${api}/files/${filePath}`, { access_token: accessToken });
2023-04-02 10:06:14 +02:00
},
async rename(fromFilePath, toFilePath, overwrite = false) {
await fetcher.put(`${origin}/api/v1/${api}/files/${fromFilePath}`, { action: 'rename', newFilePath: sanitize(toFilePath), overwrite }, { access_token: accessToken });
2023-04-12 15:59:48 +02:00
},
2023-04-25 22:45:22 +02:00
async copy(fromFilePath, toFilePath) {
await fetcher.put(`${origin}/api/v1/${api}/files/${fromFilePath}`, { action: 'copy', newFilePath: sanitize(toFilePath) }, { access_token: accessToken });
2023-04-25 22:45:22 +02:00
},
2023-04-17 17:51:19 +02:00
async chown(filePath, uid) {
await fetcher.put(`${origin}/api/v1/${api}/files/${filePath}`, { action: 'chown', uid: uid, recursive: true }, { access_token: accessToken });
2023-04-17 17:51:19 +02:00
},
async extract(path) {
await fetcher.put(`${origin}/api/v1/${api}/files/${path}`, { action: 'extract' }, { 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');
const req = new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
} else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
});
xhr.addEventListener('error', () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
});
xhr.open('POST', `${origin}/api/v1/${api}/files/${filePath}?access_token=${accessToken}&overwrite=true`);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Content-Length', file.size);
xhr.send(file);
});
await req;
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
},
2023-04-25 22:45:22 +02:00
async paste(targetDir, action, files) {
// this will not overwrite but tries to find a new unique name to past to
for (const f in files) {
let done = false;
let targetPath = targetDir + '/' + files[f].name;
while (!done) {
try {
2023-09-20 10:04:24 +02:00
if (action === 'cut') await this.rename(this.buildFilePath(files[f].folderPath, files[f].name), targetPath);
if (action === 'copy') await this.copy(this.buildFilePath(files[f].folderPath, files[f].name), targetPath);
done = true;
} catch (error) {
if (error.status === 409) {
targetPath += '-copy';
} else {
throw error;
}
}
}
2023-04-25 22:45:22 +02:00
}
},
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
};