-
Notifications
You must be signed in to change notification settings - Fork 15
/
helper.js
187 lines (148 loc) · 5.6 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* name : files/helper.js
* author : Aman Jung Karki
* created-date : 11-Feb-2020
* Description : All files related helper functionality.Including uploading file
* to cloud service.
*/
// Dependencies
let filesHelpers = require(ROOT_PATH+"/module/files/helper");
/**
* FilesHelper
* @class
*/
module.exports = class FilesHelper {
/**
* Get all signed urls.
* @method
* @name preSignedUrls
* @param {Array} payloadData - payload for files data.
* @param {String} referenceType - reference type
* @param {String} userId - Logged in user id.
* @param {String} templateId - certificateTemplateId.
* @returns {Array} - consists of all signed urls files.
*/
static preSignedUrls(payloadData, referenceType, userId = "") {
return new Promise(async (resolve, reject) => {
try {
let payloadIds = Object.keys(payloadData);
let bucketName = "";
let cloudStorage = process.env.CLOUD_STORAGE;
if( cloudStorage === "AWS" ) {
bucketName = process.env.AWS_BUCKET_NAME;
} else if (cloudStorage === "GC" ) {
bucketName = process.env.GCP_BUCKET_NAME;
} else if (cloudStorage === constants.common.ORACLE_CLOUD_SERVICE ) {
bucketName = process.env.OCI_BUCKET_NAME;
} else {
bucketName = process.env.AZURE_STORAGE_CONTAINER;
}
let result = {
[payloadIds[0]] : {}
};
if( referenceType === constants.common.PROJECT ) {
for( let pointerToPayload = 0; pointerToPayload < payloadIds.length; pointerToPayload++ ) {
let payloadId = payloadIds[pointerToPayload];
let folderPath = "project/" + payloadId + "/" + userId + "/" + gen.utils.generateUniqueId() + "/";
let imagePayload =
await filesHelpers.preSignedUrls(
payloadData[payloadId].files,
bucketName,
cloudStorage,
folderPath
);
if( !imagePayload.success ) {
return resolve({
status : httpStatusCode['bad_request'].status,
message : constants.common.FAILED_PRE_SIGNED_URL,
result : {}
});
}
if( !result[payloadId] ) {
result[payloadId] = {};
}
result[payloadId]["files"] = imagePayload.result;
}
} else {
let folderPath = "";
if (referenceType == constants.common.DHITI) {
folderPath = "reports/"
} else if (referenceType == constants.common.CERTIFICATE) {
// Folder path specifically for project certificates
folderPath = "certificateTemplates/";
} else if (referenceType == "baseTemplates") {
// Folder path specifically for project certificates
folderPath = "certificateBaseTemplates/";
} else {
folderPath = "survey/" + payloadIds[0] + "/" + userId + "/" + gen.utils.generateUniqueId() + "/";
}
let imagePayload = await filesHelpers.preSignedUrls(
payloadData[payloadIds[0]].files,
bucketName,
cloudStorage,
folderPath
);
if( !imagePayload.success ) {
return resolve({
status : httpStatusCode['bad_request'].status,
message : constants.common.FAILED_PRE_SIGNED_URL,
result : {}
});
}
result[payloadIds[0]]["files"] = imagePayload.result;
}
return resolve({
message : constants.apiResponses.URL_GENERATED,
data : result
})
} catch (error) {
return reject(error)
}
})
}
/**
* Get Downloadable URL from cloud.
* @method
* @name getDownloadableUrl
* @param {Array} payloadData - payload for files data.
* @returns {JSON} Response with status and message.
*/
static getDownloadableUrl(payloadData) {
return new Promise(async (resolve, reject) => {
try {
let bucketName = "";
let cloudStorage = process.env.CLOUD_STORAGE;
if (cloudStorage === "AWS") {
bucketName = process.env.AWS_BUCKET_NAME;
} else if (cloudStorage === "GC") {
bucketName = process.env.GCP_BUCKET_NAME;
} else if (cloudStorage === constants.common.ORACLE_CLOUD_SERVICE) {
bucketName = process.env.OCI_BUCKET_NAME;
}
else {
bucketName = process.env.AZURE_STORAGE_CONTAINER;
}
let downloadableUrl =
await filesHelpers.getDownloadableUrl(
payloadData,
bucketName,
cloudStorage
);
return resolve({
message: constants.apiResponses.CLOUD_SERVICE_SUCCESS_MESSAGE,
result: downloadableUrl
})
} catch (error) {
return reject({
status:
error.status ||
httpStatusCode["internal_server_error"].status,
message:
error.message
|| httpStatusCode["internal_server_error"].message,
errorObject: error
})
}
})
}
}