Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
server
Manage
Activity
Members
Labels
Plan
Issues
25
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cokoapps
server
Commits
8e1d0cc6
Commit
8e1d0cc6
authored
2 years ago
by
Yannis Barlas
Browse files
Options
Downloads
Patches
Plain Diff
fix(server): throw when fileStorage configuration missing
parent
877f428e
No related branches found
No related tags found
2 merge requests
!52
Docx
,
!17
Graphql api
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/services/fileStorage.js
+71
-11
71 additions, 11 deletions
src/services/fileStorage.js
with
71 additions
and
11 deletions
src/services/fileStorage.js
+
71
−
11
View file @
8e1d0cc6
...
@@ -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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment