mirror of https://github.com/tycrek/ass
parent
af51401b7b
commit
8c810592a3
@ -1 +0,0 @@
|
||||
theme: jekyll-theme-minimal
|
@ -0,0 +1,13 @@
|
||||
const fs = require('fs-extra');
|
||||
const crypto = require('crypto');
|
||||
const toArray = require('stream-to-array')
|
||||
const { path } = require('./utils');
|
||||
const { s3enabled } = require('./config.json');
|
||||
|
||||
module.exports = (file) =>
|
||||
new Promise((resolve, reject) =>
|
||||
toArray((fs.createReadStream(s3enabled ? path('uploads/', file.originalname) : path(file.path))))
|
||||
.then((parts) => Buffer.concat(parts.map((part) => Buffer.isBuffer(part) ? part : Buffer.from(part))))
|
||||
.then((buf) => crypto.createHash('sha1').update(buf).digest('hex'))
|
||||
.then(resolve)
|
||||
.catch(reject));
|
@ -1,24 +0,0 @@
|
||||
// https://docs.digitalocean.com/products/spaces/resources/s3-sdk-examples/
|
||||
// https://www.digitalocean.com/community/tutorials/how-to-upload-a-file-to-object-storage-with-node-js
|
||||
|
||||
const aws = require('aws-sdk');
|
||||
const multer = require('multer');
|
||||
const multerS3 = require('multer-s3');
|
||||
const { s3endpoint, s3bucket, s3accessKey, s3secretKey } = require('./config.json');
|
||||
|
||||
const s3 = new aws.S3({
|
||||
endpoint: new aws.Endpoint(s3endpoint),
|
||||
credentials: new aws.Credentials({ accessKeyId: s3accessKey, secretAccessKey: s3secretKey })
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage: multerS3({
|
||||
s3: s3,
|
||||
bucket: s3bucket,
|
||||
acl: 'public-read',
|
||||
key: (_req, file, cb) => cb(null, file.originalname),
|
||||
contentType: (_req, file, cb) => cb(null, file.mimetype)
|
||||
})
|
||||
}).single('file');
|
||||
|
||||
module.exports = upload;
|
@ -0,0 +1,45 @@
|
||||
// https://docs.digitalocean.com/products/spaces/resources/s3-sdk-examples/
|
||||
// https://www.digitalocean.com/community/tutorials/how-to-upload-a-file-to-object-storage-with-node-js
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const aws = require('aws-sdk');
|
||||
const multer = require('multer');
|
||||
const multerS3 = require('multer-s3');
|
||||
const { diskFilePath, saveWithDate, s3endpoint, s3bucket, s3accessKey, s3secretKey } = require('./config.json');
|
||||
|
||||
const s3 = new aws.S3({
|
||||
endpoint: new aws.Endpoint(s3endpoint),
|
||||
credentials: new aws.Credentials({ accessKeyId: s3accessKey, secretAccessKey: s3secretKey })
|
||||
});
|
||||
|
||||
const uploadS3 = multer({
|
||||
storage: multerS3({
|
||||
s3: s3,
|
||||
bucket: s3bucket,
|
||||
acl: 'public-read',
|
||||
key: (req, _file, cb) => cb(null, req.randomId),
|
||||
contentType: (_req, file, cb) => cb(null, file.mimetype)
|
||||
})
|
||||
}).single('file');
|
||||
|
||||
const uploadLocal = multer({
|
||||
storage: multer.diskStorage({
|
||||
destination: !saveWithDate ? diskFilePath : (_req, _file, cb) => {
|
||||
// Get current month and year
|
||||
let [month, _day, year] = new Date().toLocaleDateString("en-US").split("/");
|
||||
|
||||
// Add 0 before single digit months eg ( 6 turns into 06)
|
||||
let folder = `${diskFilePath}/${year}-${("0" + month).slice(-2)}`;
|
||||
|
||||
// Create folder if it doesn't exist
|
||||
fs.ensureDirSync(folder);
|
||||
|
||||
cb(null, folder);
|
||||
}
|
||||
})
|
||||
}).single('file');
|
||||
|
||||
module.exports = { uploadLocal, uploadS3 };
|
||||
|
||||
// This deletes everything from the Bucket
|
||||
//s3.listObjects({ Bucket: s3bucket }, (err, data) => err ? console.error(err) : data['Contents'].forEach((obj) => s3.deleteObject({ Bucket: s3bucket, Key: obj['Key'] }, (err, data) => err ? console.log(err) : console.log(data))));
|
Loading…
Reference in new issue