Move filemanager/ to frontend/
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
|
||||
import superagent from 'superagent';
|
||||
import { buildFilePath, sanitize } from 'pankow/utils';
|
||||
|
||||
const BASE_URL = import.meta.env.BASE_URL || '/';
|
||||
|
||||
export function createDirectoryModel(origin, accessToken, api) {
|
||||
|
||||
return {
|
||||
name: 'DirectoryModel',
|
||||
async listFiles(path) {
|
||||
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 [];
|
||||
|
||||
console.error('Failed to list files', error || result.statusCode);
|
||||
return [];
|
||||
}
|
||||
|
||||
// this prepares the entries to be compatible with all components
|
||||
result.body.entries.forEach(item => {
|
||||
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`;
|
||||
|
||||
// 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.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';
|
||||
});
|
||||
|
||||
return result.body.entries;
|
||||
},
|
||||
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))}`)
|
||||
.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 copy(fromFilePath, toFilePath) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
|
||||
.send({ action: 'copy', newFilePath: sanitize(toFilePath) })
|
||||
.query({ access_token: accessToken });
|
||||
},
|
||||
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}`);
|
||||
},
|
||||
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) {
|
||||
let result;
|
||||
try {
|
||||
result = await fetch(`${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`);
|
||||
} catch (error) {
|
||||
console.error('Failed to get file', error);
|
||||
return null;
|
||||
}
|
||||
|
||||
const text = await result.text();
|
||||
return text;
|
||||
},
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getFileUrl(path) {
|
||||
return `${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
createDirectoryModel
|
||||
};
|
||||
@@ -0,0 +1,102 @@
|
||||
|
||||
import moment from 'moment';
|
||||
import superagent from 'superagent';
|
||||
import { ansiToHtml } from 'anser';
|
||||
|
||||
// https://github.com/janl/mustache.js/blob/master/mustache.js#L60
|
||||
const entityMap = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
'\'': ''',
|
||||
'/': '/',
|
||||
'`': '`',
|
||||
'=': '='
|
||||
};
|
||||
|
||||
function escapeHtml(string) {
|
||||
return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap (s) {
|
||||
return entityMap[s];
|
||||
});
|
||||
}
|
||||
|
||||
function ab2str(buf) {
|
||||
return String.fromCharCode.apply(null, new Uint16Array(buf));
|
||||
}
|
||||
|
||||
export function create(origin, accessToken, type, id) {
|
||||
const INITIAL_STREAM_LINES = 100;
|
||||
|
||||
let eventSource = null;
|
||||
|
||||
let streamApi = '';
|
||||
let downloadApi = '';
|
||||
|
||||
if (type === 'platform') {
|
||||
streamApi = '/api/v1/cloudron/logstream/box';
|
||||
downloadApi = '/api/v1/cloudron/logs/box';
|
||||
} else if (type === 'crash') {
|
||||
streamApi = `/api/v1/cloudron/logstream/crash-${id}`;
|
||||
downloadApi = `/api/v1/cloudron/logs/crash-${id}`;
|
||||
} else if (type === 'app') {
|
||||
streamApi = `/api/v1/apps/${id}/logstream`;
|
||||
downloadApi = `/api/v1/apps/${id}/logs`;
|
||||
} else if (type === 'service') {
|
||||
streamApi = `/api/v1/services/${id}/logstream`;
|
||||
downloadApi = `/api/v1/services/${id}/logs`;
|
||||
} else if (type === 'task') {
|
||||
streamApi = `/api/v1/tasks/${id}/logstream`;
|
||||
downloadApi = `/api/v1/tasks/${id}/logs`;
|
||||
} else {
|
||||
console.error('unsupported logs type', type);
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'LogsModel',
|
||||
stream(lineHandler, errorHandler) {
|
||||
eventSource = new EventSource(`${origin}${streamApi}?lines=${INITIAL_STREAM_LINES}&access_token=${accessToken}`);
|
||||
eventSource.onerror = errorHandler;
|
||||
// eventSource.onopen = function () { console.log('stream is open'); };
|
||||
eventSource.onmessage = function (message) {
|
||||
var data;
|
||||
|
||||
try {
|
||||
data = JSON.parse(message.data);
|
||||
} catch (e) {
|
||||
return console.error(e);
|
||||
}
|
||||
|
||||
const id = data.realtimeTimestamp;
|
||||
const time = data.realtimeTimestamp ? moment(data.realtimeTimestamp/1000).format('MMM DD HH:mm:ss') : '';
|
||||
const html = ansiToHtml(escapeHtml(typeof data.message === 'string' ? data.message : ab2str(data.message)));
|
||||
|
||||
lineHandler(id, time, html);
|
||||
};
|
||||
},
|
||||
getDownloadUrl() {
|
||||
return `${origin}${downloadApi}?access_token=${accessToken}&format=short&lines=-1`;
|
||||
},
|
||||
// TODO maybe move this into AppsModel.js
|
||||
async getApp() {
|
||||
let error, result;
|
||||
try {
|
||||
result = await superagent.get(`${origin}/api/v1/apps/${id}`).query({ access_token: accessToken });
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
if (error || result.statusCode !== 200) {
|
||||
console.error(`Invalid app ${id}`, error || result.statusCode);
|
||||
this.fatalError = `Invalid app ${id}`;
|
||||
return;
|
||||
}
|
||||
|
||||
return result.body;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
create
|
||||
};
|
||||
Reference in New Issue
Block a user