mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 13:19:44 +02:00
parent
950a070589
commit
850f6df0ac
27 changed files with 2180 additions and 702 deletions
51
server/api/hooks/file-manager/LocalFileManager.js
Normal file
51
server/api/hooks/file-manager/LocalFileManager.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
const fs = require('fs');
|
||||
const fse = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { rimraf } = require('rimraf');
|
||||
|
||||
const PATH_SEGMENT_TO_URL_REPLACE_REGEX = /(public|private)\//;
|
||||
|
||||
const buildPath = (pathSegment) => path.join(sails.config.custom.uploadsBasePath, pathSegment);
|
||||
|
||||
class LocalFileManager {
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
async move(sourceFilePath, filePathSegment) {
|
||||
const { dir, base } = path.parse(filePathSegment);
|
||||
|
||||
const folderPath = buildPath(dir);
|
||||
const filePath = path.join(folderPath, base);
|
||||
|
||||
await fs.promises.mkdir(folderPath);
|
||||
await fse.move(sourceFilePath, filePath);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
async save(filePathSegment, buffer) {
|
||||
await fse.outputFile(buildPath(filePathSegment), buffer);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
read(filePathSegment) {
|
||||
const filePath = buildPath(filePathSegment);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error('File does not exist');
|
||||
}
|
||||
|
||||
return fs.createReadStream(filePath);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
async deleteFolder(folderPathSegment) {
|
||||
await rimraf(buildPath(folderPathSegment));
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
buildUrl(filePathSegment) {
|
||||
return `${sails.config.custom.baseUrl}/${filePathSegment.replace(PATH_SEGMENT_TO_URL_REPLACE_REGEX, '')}`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LocalFileManager;
|
Loading…
Add table
Add a link
Reference in a new issue