kopia lustrzana https://github.com/manuelkasper/sotlas-api
Store original photos in Backblaze (S3 compatible object storage)
rodzic
c79b215b2d
commit
a05edc7b64
|
@ -45,7 +45,6 @@ config.sotatrailsUrl = 'https://sotatrails.ch/api.php';
|
||||||
|
|
||||||
config.photos = {
|
config.photos = {
|
||||||
paths: {
|
paths: {
|
||||||
original: '/data/images/photos/original',
|
|
||||||
thumb: '/data/images/photos/thumb',
|
thumb: '/data/images/photos/thumb',
|
||||||
large: '/data/images/photos/large'
|
large: '/data/images/photos/large'
|
||||||
},
|
},
|
||||||
|
@ -59,7 +58,12 @@ config.photos = {
|
||||||
height: 256
|
height: 256
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
uploadPath: '/data/upload/photos'
|
uploadPath: '/data/upload/photos',
|
||||||
|
originalStorage: {
|
||||||
|
endPoint: 's3.eu-central-003.backblazeb2.com',
|
||||||
|
accessKey: 'xxxxxxxxxxxxxxxxx',
|
||||||
|
secretKey: 'xxxxxxxxxxxxxxxxx'
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
config.tracks = {
|
config.tracks = {
|
||||||
|
|
Plik diff jest za duży
Load Diff
|
@ -26,6 +26,7 @@
|
||||||
"htmlparser2": "^3.10.1",
|
"htmlparser2": "^3.10.1",
|
||||||
"jwks-rsa": "^1.6.0",
|
"jwks-rsa": "^1.6.0",
|
||||||
"maxmind": "^3.1.2",
|
"maxmind": "^3.1.2",
|
||||||
|
"minio": "^7.0.29",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
"mongodb": "^3.6.1",
|
"mongodb": "^3.6.1",
|
||||||
"multer": "^1.4.2",
|
"multer": "^1.4.2",
|
||||||
|
|
22
photos.js
22
photos.js
|
@ -5,6 +5,8 @@ const fsPromises = require('fs').promises
|
||||||
const exif = require('exif-reader')
|
const exif = require('exif-reader')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const hasha = require('hasha')
|
const hasha = require('hasha')
|
||||||
|
const minio = require('minio')
|
||||||
|
const nodemailer = require('nodemailer')
|
||||||
const config = require('./config')
|
const config = require('./config')
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
|
|
||||||
|
@ -13,15 +15,27 @@ module.exports = {
|
||||||
// Hash input file to determine filename
|
// Hash input file to determine filename
|
||||||
let hash = await hasha.fromFile(filename, {algorithm: 'sha256'})
|
let hash = await hasha.fromFile(filename, {algorithm: 'sha256'})
|
||||||
let hashFilename = hash.substr(0, 32) + '.jpg'
|
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)
|
let metadata = await getMetadata(filename)
|
||||||
if (metadata.format !== 'jpeg' && metadata.format != 'png' && metadata.format != 'heif') {
|
if (metadata.format !== 'jpeg' && metadata.format != 'png' && metadata.format != 'heif') {
|
||||||
throw new Error('Bad input format, must be JPEG, PNG or 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 = {
|
let photo = {
|
||||||
filename: hashFilename,
|
filename: hashFilename,
|
||||||
|
@ -84,7 +98,7 @@ module.exports = {
|
||||||
Object.keys(config.photos.sizes).forEach(sizeDescr => {
|
Object.keys(config.photos.sizes).forEach(sizeDescr => {
|
||||||
let outPath = config.photos.paths[sizeDescr] + '/' + hashFilename.substr(0, 2) + '/' + hashFilename
|
let outPath = config.photos.paths[sizeDescr] + '/' + hashFilename.substr(0, 2) + '/' + hashFilename
|
||||||
mkdirTasks.push(fsPromises.mkdir(path.dirname(outPath), {recursive: true}))
|
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)
|
await Promise.all(mkdirTasks)
|
||||||
|
|
Ładowanie…
Reference in New Issue