|
|
@@ -15,10 +15,7 @@ module.exports = function(crowi) {
|
|
|
return configManager.getConfig('crowi', 'gcs:bucket');
|
|
|
}
|
|
|
|
|
|
- function getGcsInstance(isUploadable) {
|
|
|
- if (!isUploadable) {
|
|
|
- throw new Error('GCS is not configured.');
|
|
|
- }
|
|
|
+ function getGcsInstance() {
|
|
|
if (_instance == null) {
|
|
|
const keyFilename = configManager.getConfig('crowi', 'gcs:apiKeyJsonPath');
|
|
|
// see https://googleapis.dev/nodejs/storage/latest/Storage.html
|
|
|
@@ -59,7 +56,11 @@ module.exports = function(crowi) {
|
|
|
};
|
|
|
|
|
|
lib.deleteFileByFilePath = async function(filePath) {
|
|
|
- const gcs = getGcsInstance(this.getIsUploadable());
|
|
|
+ if (!this.getIsUploadable()) {
|
|
|
+ throw new Error('GCS is not configured.');
|
|
|
+ }
|
|
|
+
|
|
|
+ const gcs = getGcsInstance();
|
|
|
const myBucket = gcs.bucket(getGcsBucket());
|
|
|
const file = myBucket.file(filePath);
|
|
|
|
|
|
@@ -74,9 +75,13 @@ module.exports = function(crowi) {
|
|
|
};
|
|
|
|
|
|
lib.uploadFile = function(fileStream, attachment) {
|
|
|
+ if (!this.getIsUploadable()) {
|
|
|
+ throw new Error('GCS is not configured.');
|
|
|
+ }
|
|
|
+
|
|
|
logger.debug(`File uploading: fileName=${attachment.fileName}`);
|
|
|
|
|
|
- const gcs = getGcsInstance(this.getIsUploadable());
|
|
|
+ const gcs = getGcsInstance();
|
|
|
const myBucket = gcs.bucket(getGcsBucket());
|
|
|
const filePath = getFilePathOnStorage(attachment);
|
|
|
const options = {
|
|
|
@@ -93,7 +98,11 @@ module.exports = function(crowi) {
|
|
|
* @return {stream.Readable} readable stream
|
|
|
*/
|
|
|
lib.findDeliveryFile = async function(attachment) {
|
|
|
- const gcs = getGcsInstance(this.getIsUploadable());
|
|
|
+ if (!this.getIsReadable()) {
|
|
|
+ throw new Error('GCS is not configured.');
|
|
|
+ }
|
|
|
+
|
|
|
+ const gcs = getGcsInstance();
|
|
|
const myBucket = gcs.bucket(getGcsBucket());
|
|
|
const filePath = getFilePathOnStorage(attachment);
|
|
|
const file = myBucket.file(filePath);
|