Store original photos in Backblaze (S3 compatible object storage)

master
Manuel Kasper 2022-08-02 13:34:32 +02:00
rodzic c79b215b2d
commit a05edc7b64
4 zmienionych plików z 4729 dodań i 18 usunięć

Wyświetl plik

@ -45,7 +45,6 @@ config.sotatrailsUrl = 'https://sotatrails.ch/api.php';
config.photos = {
paths: {
original: '/data/images/photos/original',
thumb: '/data/images/photos/thumb',
large: '/data/images/photos/large'
},
@ -59,7 +58,12 @@ config.photos = {
height: 256
}
},
uploadPath: '/data/upload/photos'
uploadPath: '/data/upload/photos',
originalStorage: {
endPoint: 's3.eu-central-003.backblazeb2.com',
accessKey: 'xxxxxxxxxxxxxxxxx',
secretKey: 'xxxxxxxxxxxxxxxxx'
}
};
config.tracks = {

4716
package-lock.json wygenerowano

Plik diff jest za duży Load Diff

Wyświetl plik

@ -26,6 +26,7 @@
"htmlparser2": "^3.10.1",
"jwks-rsa": "^1.6.0",
"maxmind": "^3.1.2",
"minio": "^7.0.29",
"moment": "^2.24.0",
"mongodb": "^3.6.1",
"multer": "^1.4.2",

Wyświetl plik

@ -5,6 +5,8 @@ const fsPromises = require('fs').promises
const exif = require('exif-reader')
const path = require('path')
const hasha = require('hasha')
const minio = require('minio')
const nodemailer = require('nodemailer')
const config = require('./config')
const db = require('./db')
@ -13,15 +15,27 @@ module.exports = {
// Hash input file to determine filename
let hash = await hasha.fromFile(filename, {algorithm: 'sha256'})
let hashFilename = hash.substr(0, 32) + '.jpg'
let originalPath = config.photos.paths.original + '/' + hashFilename.substr(0, 2) + '/' + hashFilename
await fsPromises.mkdir(path.dirname(originalPath), {recursive: true})
let metadata = await getMetadata(filename)
if (metadata.format !== 'jpeg' && metadata.format != 'png' && metadata.format != 'heif') {
throw new Error('Bad input format, must be JPEG, PNG or HEIF')
}
await fsPromises.copyFile(filename, originalPath)
// Upload original photo to Backblaze (don't wait for completion)
let minioClient = new minio.Client(config.photos.originalStorage)
minioClient.fPutObject('sotlas', hashFilename, filename, {'Content-Type': 'image/jpeg'}, (err, etag) => {
if (err) {
console.error(err)
let transporter = nodemailer.createTransport(config.mail)
transporter.sendMail({
from: 'api@sotl.as',
to: 'mk@neon1.net',
subject: 'Backblaze upload failed',
text: `The file ${filename} could not be uploaded:\n${err}`
})
}
})
let photo = {
filename: hashFilename,
@ -84,7 +98,7 @@ module.exports = {
Object.keys(config.photos.sizes).forEach(sizeDescr => {
let outPath = config.photos.paths[sizeDescr] + '/' + hashFilename.substr(0, 2) + '/' + hashFilename
mkdirTasks.push(fsPromises.mkdir(path.dirname(outPath), {recursive: true}))
resizeTasks.push(makeResized(originalPath, outPath, config.photos.sizes[sizeDescr].width, config.photos.sizes[sizeDescr].height))
resizeTasks.push(makeResized(filename, outPath, config.photos.sizes[sizeDescr].width, config.photos.sizes[sizeDescr].height))
})
await Promise.all(mkdirTasks)