1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-04 21:15:25 +02:00

support attachments s3

This commit is contained in:
Nguyễn Hải Quang 2024-11-09 18:49:26 +07:00
parent f6ea10df97
commit 84b0f91b44
10 changed files with 392 additions and 14 deletions

View file

@ -51,7 +51,18 @@ module.exports = {
if (attachment) {
try {
rimraf.sync(path.join(sails.config.custom.attachmentsPath, attachment.dirname));
const type = attachment.type || 'local';
if (type === 's3') {
const client = await sails.helpers.attachments.getSimpleStorageServiceClient();
if (client) {
const file1 = `${attachment.dirname}/${attachment.filename}`;
const file2 = `${attachment.dirname}/thumbnails/cover-256.png`;
await client.delete({ Key: file1 });
await client.delete({ Key: file2 });
}
} else {
rimraf.sync(path.join(sails.config.custom.attachmentsPath, attachment.dirname));
}
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}

View file

@ -0,0 +1,45 @@
const AWS = require('aws-sdk');
class S3Client {
constructor(options) {
AWS.config.update({
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
region: options.region,
});
this.bucket = options.bucket;
this.client = new AWS.S3({
endpoint: options.endpoint,
});
}
upload({ Key, Body, ContentType }) {
return this.client
.upload({
Bucket: this.bucket,
Key,
Body,
ContentType,
ACL: 'public-read',
})
.promise();
}
delete({ Key }) {
return this.client
.deleteObject({
Bucket: this.bucket,
Key,
})
.promise();
}
}
module.exports = {
fn() {
if (sails.config.custom.attachmentsS3) {
return new S3Client(sails.config.custom.attachmentsS3);
}
return null;
},
};

View file

@ -23,6 +23,69 @@ module.exports = {
const rootPath = path.join(sails.config.custom.attachmentsPath, dirname);
const filePath = path.join(rootPath, filename);
if (sails.config.custom.attachmentsS3) {
const client = await sails.helpers.attachments.getSimpleStorageServiceClient();
const s3Image = await client.upload({
Body: fs.createReadStream(inputs.file.fd),
Key: `${dirname}/${filename}`,
ContentType: inputs.file.type,
});
let image = sharp(inputs.file.fd, {
animated: true,
});
let metadata;
try {
metadata = await image.metadata();
} catch (error) {} // eslint-disable-line no-empty
const fileData = {
type: 's3',
dirname,
filename,
thumb: null,
image: null,
url: s3Image.Location,
name: inputs.file.filename,
};
if (metadata && !['svg', 'pdf'].includes(metadata.format)) {
let { width, pageHeight: height = metadata.height } = metadata;
if (metadata.orientation && metadata.orientation > 4) {
[image, width, height] = [image.rotate(), height, width];
}
const isPortrait = height > width;
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
try {
const resizeBuffer = await image
.resize(
256,
isPortrait ? 320 : undefined,
width < 256 || (isPortrait && height < 320)
? {
kernel: sharp.kernel.nearest,
}
: undefined,
)
.toBuffer();
const s3Thumb = await client.upload({
Key: `${dirname}/thumbnails/cover-256.${thumbnailsExtension}`,
Body: resizeBuffer,
ContentType: inputs.file.type,
});
fileData.thumb = s3Thumb.Location;
fileData.image = { width, height };
} catch (error1) {
console.warn(error2.stack); // eslint-disable-line no-console
}
}
return fileData;
}
fs.mkdirSync(rootPath);
await moveFile(inputs.file.fd, filePath);
@ -36,6 +99,7 @@ module.exports = {
} catch (error) {} // eslint-disable-line no-empty
const fileData = {
type: 'local',
dirname,
filename,
image: null,