import Planka upstream

This commit is contained in:
xiaojibeier
2026-07-18 11:48:22 +08:00
commit c26fd3b190
1564 changed files with 214813 additions and 0 deletions
@@ -0,0 +1,138 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const { pipeline } = require('stream/promises');
const mime = require('mime-types');
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 dirPath = buildPath(dir);
const filePath = path.join(dirPath, base);
await fs.promises.mkdir(dirPath, { recursive: true });
await fse.move(sourceFilePath, filePath);
return filePath;
}
// eslint-disable-next-line class-methods-use-this
async save(filePathSegment, stream) {
const filePath = buildPath(filePathSegment);
const { dir: dirPath } = path.parse(filePath);
await fs.promises.mkdir(dirPath, { recursive: true });
await pipeline(stream, fs.createWriteStream(filePath));
}
// eslint-disable-next-line class-methods-use-this
async read(filePathSegment, { withHeaders = false } = {}) {
const filePath = buildPath(filePathSegment);
let stat;
try {
stat = await fs.promises.stat(filePath);
} catch (error) {
throw new Error('File does not exist');
}
const readStream = fs.createReadStream(filePath);
if (withHeaders) {
return [
readStream,
{
'Content-Type': mime.lookup(filePathSegment) || 'application/octet-stream',
'Content-Length': stat.size,
'Last-Modified': stat.mtime.toUTCString(),
ETag: `W/"${stat.size.toString(16)}-${stat.mtime.getTime().toString(16)}"`,
'Accept-Ranges': 'bytes',
},
];
}
return readStream;
}
// eslint-disable-next-line class-methods-use-this
async getSize(filePathSegment) {
let stat;
try {
stat = await fs.promises.stat(buildPath(filePathSegment));
} catch (error) {
return null;
}
return stat.size;
}
// eslint-disable-next-line class-methods-use-this
async rename(filePathSegment, nextFilePathSegment) {
try {
await fs.promises.rename(buildPath(filePathSegment), buildPath(nextFilePathSegment));
} catch (error) {
/* empty */
}
}
// eslint-disable-next-line class-methods-use-this
async delete(filePathSegment) {
try {
await fs.promises.unlink(buildPath(filePathSegment));
} catch (error) {
/* empty */
}
}
// eslint-disable-next-line class-methods-use-this
async listDir(dirPathSegment) {
let dirents;
try {
dirents = await fs.promises.readdir(buildPath(dirPathSegment), {
withFileTypes: true,
});
} catch (error) {
return null;
}
return dirents.flatMap((dirent) => (dirent.isDirectory() ? dirent.name : []));
}
// eslint-disable-next-line class-methods-use-this
async renameDir(dirPathSegment, nextDirPathSegment) {
try {
await fs.promises.rename(buildPath(dirPathSegment), buildPath(nextDirPathSegment));
} catch (error) {
/* empty */
}
}
// eslint-disable-next-line class-methods-use-this
async deleteDir(dirPathSegment) {
await rimraf(buildPath(dirPathSegment));
}
// eslint-disable-next-line class-methods-use-this
async isExists(pathSegment) {
return fse.pathExists(buildPath(pathSegment));
}
/* // 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;
@@ -0,0 +1,233 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const fs = require('fs');
const mime = require('mime-types');
const {
CopyObjectCommand,
DeleteObjectsCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
ListObjectsV2Command,
PutObjectCommand,
} = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
class S3FileManager {
constructor(client) {
this.client = client;
}
async move(sourceFilePath, filePathSegment, contentType) {
const command = new PutObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
Body: fs.createReadStream(sourceFilePath),
ContentType: contentType,
});
await this.client.send(command);
return null;
}
async save(filePathSegment, stream, contentType) {
const upload = new Upload({
client: this.client,
params: {
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
Body: stream,
ContentType: contentType,
},
});
await upload.done();
}
async read(filePathSegment, { withHeaders = false } = {}) {
const command = new GetObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
});
const result = await this.client.send(command);
if (withHeaders) {
const headers = {
'Content-Type':
result.ContentType || mime.lookup(filePathSegment) || 'application/octet-stream',
'Accept-Ranges': result.AcceptRanges || 'bytes',
};
if (!_.isUndefined(result.ContentLength)) {
headers['Content-Length'] = result.ContentLength;
}
if (!_.isUndefined(result.LastModified)) {
headers['Last-Modified'] = result.LastModified.toUTCString();
}
if (_.isUndefined(result.ETag)) {
if (!_.isUndefined(result.ContentLength) && !_.isUndefined(result.LastModified)) {
headers.ETag = `W/"${result.ContentLength.toString(16)}-${result.LastModified.getTime().toString(16)}"`;
}
} else {
headers.ETag = result.ETag;
}
return [result.Body, headers];
}
return result.Body;
}
async getSize(filePathSegment) {
const headObjectCommand = new HeadObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
});
let result;
try {
result = await this.client.send(headObjectCommand);
} catch (error) {
return null;
}
return result.ContentLength;
}
async rename(filePathSegment, nextFilePathSegment) {
const copyObjectCommand = new CopyObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: nextFilePathSegment,
CopySource: `${sails.config.custom.s3Bucket}/${filePathSegment}`,
});
try {
await this.client.send(copyObjectCommand);
} catch (error) {
return;
}
await this.delete(filePathSegment);
}
async delete(filePathSegment) {
const deleteObjectCommand = new DeleteObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
});
await this.client.send(deleteObjectCommand);
}
async listDir(dirPathSegment, { ContinuationToken } = {}) {
const listObjectsCommand = new ListObjectsV2Command({
ContinuationToken,
Bucket: sails.config.custom.s3Bucket,
Prefix: `${dirPathSegment}/`,
Delimiter: '/',
});
const result = await this.client.send(listObjectsCommand);
if (!result.CommonPrefixes) {
return null;
}
const dirnames = result.CommonPrefixes.map(({ Prefix }) =>
Prefix.slice(dirPathSegment.length + 1, -1),
);
if (result.IsTruncated) {
const otherDirnames = await this.listDir(dirPathSegment, {
ContinuationToken: result.NextContinuationToken,
});
if (otherDirnames) {
dirnames.push(...otherDirnames);
}
}
return dirnames;
}
async renameDir(dirPathSegment, nextDirPathSegment, { ContinuationToken } = {}) {
const listObjectsCommand = new ListObjectsV2Command({
ContinuationToken,
Bucket: sails.config.custom.s3Bucket,
Prefix: dirPathSegment,
});
const result = await this.client.send(listObjectsCommand);
if (!result.Contents) {
return;
}
// eslint-disable-next-line no-restricted-syntax
for (const { Key } of result.Contents) {
// eslint-disable-next-line no-await-in-loop
await this.rename(Key, `${nextDirPathSegment}/${Key.substring(dirPathSegment.length + 1)}`);
}
if (result.IsTruncated) {
await this.renameDir(dirPathSegment, nextDirPathSegment, {
ContinuationToken: result.NextContinuationToken,
});
}
}
async deleteDir(dirPathSegment, { ContinuationToken } = {}) {
const listObjectsCommand = new ListObjectsV2Command({
ContinuationToken,
Bucket: sails.config.custom.s3Bucket,
Prefix: dirPathSegment,
});
const result = await this.client.send(listObjectsCommand);
if (!result.Contents) {
return;
}
if (result.Contents.length > 0) {
const deleteObjectsCommand = new DeleteObjectsCommand({
Bucket: sails.config.custom.s3Bucket,
Delete: {
Objects: result.Contents.map(({ Key }) => ({ Key })),
},
});
await this.client.send(deleteObjectsCommand);
}
if (result.IsTruncated) {
await this.deleteDir(dirPathSegment, {
ContinuationToken: result.NextContinuationToken,
});
}
}
async isExists(pathSegment) {
const listObjectsCommand = new ListObjectsV2Command({
Bucket: sails.config.custom.s3Bucket,
Prefix: pathSegment,
MaxKeys: 1,
});
const result = await this.client.send(listObjectsCommand);
return !!result.Contents && result.Contents.length === 1;
}
/* // eslint-disable-next-line class-methods-use-this
buildUrl(filePathSegment) {
return `${sails.hooks.s3.getBaseUrl()}/${filePathSegment}`;
} */
}
module.exports = S3FileManager;
@@ -0,0 +1,46 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
/**
* file-manager hook
*
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions,
* and/or initialization logic.
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
*/
const LocalFileManager = require('./LocalFileManager');
const S3FileManager = require('./S3FileManager');
module.exports = function defineFileManagerHook(sails) {
let instance = null;
const createInstance = () => {
instance = sails.hooks.s3.isEnabled()
? new S3FileManager(sails.hooks.s3.getClient())
: new LocalFileManager();
};
return {
/**
* Runs when this Sails app loads/lifts.
*/
async initialize() {
sails.log.info('Initializing custom hook (`file-manager`)');
return new Promise((resolve) => {
sails.after('hook:s3:loaded', () => {
createInstance();
resolve();
});
});
},
getInstance() {
return instance;
},
};
};