| 264 | |
| 265 | // this method is used in Comment and Message models |
| 266 | async function moveFile({ |
| 267 | fileUrl, |
| 268 | fileName, |
| 269 | teamId, |
| 270 | userId, |
| 271 | discussionId, |
| 272 | commentId, |
| 273 | chatId, |
| 274 | messageId, |
| 275 | }: { |
| 276 | fileUrl: string; |
| 277 | fileName: string; |
| 278 | teamId: string; |
| 279 | userId: string; |
| 280 | discussionId?: string; |
| 281 | commentId?: string; |
| 282 | chatId?: string; |
| 283 | messageId?: string; |
| 284 | }) { |
| 285 | aws.config.update({ |
| 286 | region: process.env.AWS_REGION, |
| 287 | accessKeyId: process.env.AWS_ACCESSKEYID, |
| 288 | secretAccessKey: process.env.AWS_SECRETACCESSKEY, |
| 289 | }); |
| 290 | |
| 291 | const s3 = new aws.S3({ apiVersion: 'latest' }); |
| 292 | |
| 293 | const randomString20 = |
| 294 | Math.random().toString(36).substring(2, 12) + Math.random().toString(36).substring(2, 12); |
| 295 | |
| 296 | const fileExt = pathModule.extname(fileName); |
| 297 | const fileNameWithoutExtension = pathModule.basename(fileName, fileExt); |
| 298 | |
| 299 | let key: string; |
| 300 | if (discussionId && !chatId) { |
| 301 | key = `team-${teamId}/discussion-${discussionId}/comment-${commentId}/user-${userId}/${randomString20}/${slugify( |
| 302 | fileNameWithoutExtension, |
| 303 | )}${fileExt}`; |
| 304 | } else if (!discussionId && chatId) { |
| 305 | key = `team-${teamId}/chat-${chatId}/message-${messageId}/user-${userId}/${randomString20}/${slugify( |
| 306 | fileNameWithoutExtension, |
| 307 | )}${fileExt}`; |
| 308 | } |
| 309 | |
| 310 | const copyParams = { |
| 311 | Bucket: process.env.BUCKET_FOR_FILES, |
| 312 | CopySource: process.env.BUCKET_FOR_FILES + '/' + fileUrl.split('.s3.amazonaws.com/')[1], |
| 313 | Key: key, |
| 314 | ACL: 'public-read', |
| 315 | }; |
| 316 | |
| 317 | await s3.copyObject(copyParams).promise(); |
| 318 | |
| 319 | const deleteParams = { |
| 320 | Bucket: process.env.BUCKET_FOR_FILES, |
| 321 | Key: fileUrl.split('.s3.amazonaws.com/')[1], |
| 322 | }; |
| 323 | |