-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 2 additions & 45 deletions
47
packages/evershop/src/modules/cms/api/imageUpload/[context]multerFile[auth].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/evershop/src/modules/cms/services/CustomMemoryStorage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
/* eslint-disable no-param-reassign */ | ||
const concat = require('concat-stream'); | ||
|
||
function CustomMemoryStorage(opts) { | ||
this.getFilename = opts.filename; | ||
} | ||
|
||
CustomMemoryStorage.prototype._handleFile = function _handleFile( | ||
req, | ||
file, | ||
cb | ||
) { | ||
const filename = this.getFilename(file.originalname); | ||
file.stream.pipe( | ||
concat({ encoding: 'buffer' }, (data) => { | ||
cb(null, { | ||
buffer: data, | ||
size: data.length, | ||
filename | ||
}); | ||
}) | ||
); | ||
}; | ||
|
||
CustomMemoryStorage.prototype._removeFile = function _removeFile( | ||
req, | ||
file, | ||
cb | ||
) { | ||
delete file.buffer; | ||
cb(null); | ||
}; | ||
|
||
module.exports = function (opts) { | ||
return new CustomMemoryStorage(opts); | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/evershop/src/modules/cms/services/generateFileName.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const path = require('path'); | ||
|
||
module.exports.generateFileName = (originalname) => { | ||
const extension = path.extname(originalname); | ||
const name = path.basename(originalname, extension); | ||
// Replace special characters and white spaces with a - | ||
const fileName = name.replace(/[^a-z0-9]/gi, '-').toLowerCase(); | ||
return `${fileName}${extension}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const { mkdirSync } = require('fs'); | ||
const { join } = require('path'); | ||
const multer = require('multer'); | ||
const { getConfig } = require('@evershop/evershop/src/lib/util/getConfig'); | ||
const { CONSTANTS } = require('@evershop/evershop/src/lib/helpers'); | ||
const { generateFileName } = require('./generateFileName'); | ||
const customMemoryStorage = require('./CustomMemoryStorage'); | ||
|
||
const filename = (request, file, cb) => { | ||
const fileName = generateFileName(file.originalname); | ||
cb(null, fileName); | ||
}; | ||
|
||
function fileFilter(request, file, cb) { | ||
const allowedMimeTypes = getConfig('allowed_mime_types', [ | ||
'image/jpeg', | ||
'image/png', | ||
'image/gif', | ||
'image/webp', | ||
'image/avif', | ||
'image/apng' | ||
]); | ||
// Only accept images | ||
if (!allowedMimeTypes.includes(file.mimetype)) { | ||
return cb( | ||
new Error( | ||
"Only 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/apng' files are allowed" | ||
) | ||
); | ||
} else { | ||
return cb(null, true); | ||
} | ||
} | ||
const diskStorage = multer.diskStorage({ | ||
destination(request, file, cb) { | ||
const path = join( | ||
CONSTANTS.MEDIAPATH, | ||
(request.params[0] || '').replace(/\s/g, '-') | ||
); | ||
mkdirSync(path, { recursive: true }); | ||
cb(null, path); | ||
}, | ||
filename | ||
}); | ||
|
||
module.exports.getMulter = () => { | ||
const storageProvider = getConfig('file_storage', 'local'); | ||
|
||
if (storageProvider === 'local') { | ||
return multer({ storage: diskStorage, fileFilter }); | ||
} else { | ||
const memoryStorage = customMemoryStorage({ filename: generateFileName }); | ||
return multer({ storage: memoryStorage, fileFilter }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters