Skip to content
Snippets Groups Projects
Commit 8e1d0cc6 authored by Yannis Barlas's avatar Yannis Barlas
Browse files

fix(server): throw when fileStorage configuration missing

parent 877f428e
No related branches found
No related tags found
2 merge requests!52Docx,!17Graphql api
...@@ -8,17 +8,6 @@ const path = require('path') ...@@ -8,17 +8,6 @@ const path = require('path')
const mime = require('mime-types') const mime = require('mime-types')
const logger = require('@pubsweet/logger') const logger = require('@pubsweet/logger')
const {
accessKeyId,
secretAccessKey,
bucket,
protocol,
host,
port,
maximumWidthForSmallImages,
maximumWidthForMediumImages,
} = config.get('fileStorage')
const { const {
convertFileStreamIntoBuffer, convertFileStreamIntoBuffer,
getFileExtension, getFileExtension,
...@@ -33,6 +22,14 @@ let s3 ...@@ -33,6 +22,14 @@ let s3
const healthCheck = () => { const healthCheck = () => {
try { try {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
s3.getBucketLogging({ Bucket: bucket }, (err, data) => { s3.getBucketLogging({ Bucket: bucket }, (err, data) => {
if (err) { if (err) {
...@@ -55,6 +52,21 @@ if ( ...@@ -55,6 +52,21 @@ if (
config.has('pubsweet-server.useFileStorage') && config.has('pubsweet-server.useFileStorage') &&
config.get('pubsweet-server.useFileStorage') config.get('pubsweet-server.useFileStorage')
) { ) {
if (!config.has('fileStorage')) {
throw new Error(
'you have declared that you will use file storage but fileStorage configuration is missing',
)
}
const {
accessKeyId,
secretAccessKey,
bucket,
protocol,
host,
port,
} = config.get('fileStorage')
if (!protocol) { if (!protocol) {
throw new Error( throw new Error(
'missing required protocol param for initializing file storage', 'missing required protocol param for initializing file storage',
...@@ -104,6 +116,11 @@ const createImageVersions = async ( ...@@ -104,6 +116,11 @@ const createImageVersions = async (
isSVG, isSVG,
) => { ) => {
try { try {
const {
maximumWidthForSmallImages,
maximumWidthForMediumImages,
} = config.get('fileStorage')
const mediumWidth = maximumWidthForMediumImages const mediumWidth = maximumWidthForMediumImages
? parseInt(maximumWidthForMediumImages, 10) ? parseInt(maximumWidthForMediumImages, 10)
: 640 : 640
...@@ -155,6 +172,13 @@ const createImageVersions = async ( ...@@ -155,6 +172,13 @@ const createImageVersions = async (
} }
const getURL = async (objectKey, options = {}) => { const getURL = async (objectKey, options = {}) => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
const { expiresIn } = options const { expiresIn } = options
const s3Params = { const s3Params = {
...@@ -167,6 +191,13 @@ const getURL = async (objectKey, options = {}) => { ...@@ -167,6 +191,13 @@ const getURL = async (objectKey, options = {}) => {
} }
const uploadFileHandler = (fileStream, filename, mimetype) => { const uploadFileHandler = (fileStream, filename, mimetype) => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
const params = { const params = {
Bucket: bucket, Bucket: bucket,
Key: filename, // file name you want to save as Key: filename, // file name you want to save as
...@@ -371,6 +402,13 @@ const upload = async (fileStream, filename, options = {}) => { ...@@ -371,6 +402,13 @@ const upload = async (fileStream, filename, options = {}) => {
} }
const getFileInfo = key => { const getFileInfo = key => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
const params = { const params = {
Bucket: bucket, Bucket: bucket,
Key: key, Key: key,
...@@ -388,6 +426,13 @@ const getFileInfo = key => { ...@@ -388,6 +426,13 @@ const getFileInfo = key => {
} }
const list = () => { const list = () => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
const params = { const params = {
Bucket: bucket, Bucket: bucket,
} }
...@@ -405,6 +450,14 @@ const list = () => { ...@@ -405,6 +450,14 @@ const list = () => {
// Accepts an array of object keys // Accepts an array of object keys
const deleteFiles = objectKeys => { const deleteFiles = objectKeys => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
if (objectKeys.length === 0) { if (objectKeys.length === 0) {
throw new Error('the provided array of keys if empty') throw new Error('the provided array of keys if empty')
} }
...@@ -433,6 +486,13 @@ const deleteFiles = objectKeys => { ...@@ -433,6 +486,13 @@ const deleteFiles = objectKeys => {
} }
const download = (key, localPath) => { const download = (key, localPath) => {
if (!s3) {
throw new Error(
's3 does not exist! Probably configuration is missing/invalid',
)
}
const { bucket } = config.get('fileStorage')
const fileStream = fs.createWriteStream(localPath) const fileStream = fs.createWriteStream(localPath)
const s3Stream = s3.getObject({ Bucket: bucket, Key: key }).createReadStream() const s3Stream = s3.getObject({ Bucket: bucket, Key: key }).createReadStream()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment