@tklein1801/cap-js-attachments
Advanced tools
+35
-35
| const { S3Client, GetObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3'); | ||
| const { Upload } = require("@aws-sdk/lib-storage"); | ||
| const { scanRequest } = require('./malwareScanner') | ||
| const cds = require("@sap/cds"); | ||
| const { Upload } = require('@aws-sdk/lib-storage'); | ||
| const { scanRequest } = require('./malwareScanner'); | ||
| const cds = require('@sap/cds'); | ||
| const logger = cds.log('attachments'); | ||
| const { SELECT } = cds.ql; | ||
| module.exports = class AWSAttachmentsService extends require("./basic") { | ||
| module.exports = class AWSAttachmentsService extends require('./basic') { | ||
| init() { | ||
@@ -12,8 +13,11 @@ const creds = { | ||
| region: process.env.ATTACHMENTS_AWS_REGION || this.options.credentials?.region, | ||
| access_key_id: process.env.ATTACHMENTS_AWS_ACCESS_KEY_ID || this.options.credentials?.access_key_id, | ||
| secret_access_key: process.env.ATTACHMENTS_AWS_SECRET_ACCESS_KEY || this.options.credentials?.secret_access_key, | ||
| access_key_id: | ||
| process.env.ATTACHMENTS_AWS_ACCESS_KEY_ID || this.options.credentials?.access_key_id, | ||
| secret_access_key: | ||
| process.env.ATTACHMENTS_AWS_SECRET_ACCESS_KEY || | ||
| this.options.credentials?.secret_access_key, | ||
| }; | ||
| if(!creds.bucket || !creds.region || !creds.access_key_id || !creds.secret_access_key) { | ||
| throw new Error("SAP Object Store instance is not bound or missing environment variables."); | ||
| if (!creds.bucket || !creds.region || !creds.access_key_id || !creds.secret_access_key) { | ||
| throw new Error('SAP Object Store instance is not bound or missing environment variables.'); | ||
| } | ||
@@ -28,3 +32,3 @@ | ||
| }, | ||
| }); | ||
| }); | ||
| return super.init(); | ||
@@ -34,6 +38,3 @@ } | ||
| async put(attachments, data, _content) { | ||
| if (Array.isArray(data)) | ||
| return Promise.all( | ||
| data.map((d) => this.put(attachments, d)) | ||
| ); | ||
| if (Array.isArray(data)) return Promise.all(data.map((d) => this.put(attachments, d))); | ||
| const { content = _content, ...metadata } = data; | ||
@@ -54,5 +55,5 @@ const Key = metadata.url; | ||
| await Promise.all([stored, multipartUpload.done()]); | ||
| if(this.kind === 's3') scanRequest(attachments, { ID: metadata.ID }) | ||
| if (this.kind === 's3') scanRequest(attachments, { ID: metadata.ID }); | ||
| } catch (err) { | ||
| console.error(err); // eslint-disable-line no-console | ||
| logger.error(err); | ||
| } | ||
@@ -63,3 +64,3 @@ } | ||
| async get(attachments, keys, req = {}) { | ||
| const response = await SELECT.from(attachments, keys).columns("url"); | ||
| const response = await SELECT.from(attachments, keys).columns('url'); | ||
| if (response?.url) { | ||
@@ -83,3 +84,3 @@ const Key = response.url; | ||
| async deleteAttachmentsWithKeys(records, req) { | ||
| if (req?.attachmentsToDelete?.length>0) { | ||
| if (req?.attachmentsToDelete?.length > 0) { | ||
| req.attachmentsToDelete.forEach((attachment) => { | ||
@@ -92,8 +93,9 @@ this.deleteAttachment(attachment.url); | ||
| async attachDeletionData(req) { | ||
| const attachments = cds.model.definitions[req.query.target.name + ".attachments"]; | ||
| const attachments = cds.model.definitions[req.query.target.name + '.attachments']; | ||
| if (attachments) { | ||
| const diffData = await req.diff(); | ||
| let deletedAttachments = []; | ||
| diffData.attachments?.filter((object) => { | ||
| return object._op === "delete"; | ||
| diffData.attachments | ||
| ?.filter((object) => { | ||
| return object._op === 'delete'; | ||
| }) | ||
@@ -104,4 +106,6 @@ .map((attachment) => { | ||
| if(deletedAttachments.length > 0) { | ||
| let attachmentsToDelete = await SELECT.from(attachments).columns("url").where({ ID: { in: [...deletedAttachments] } }); | ||
| if (deletedAttachments.length > 0) { | ||
| let attachmentsToDelete = await SELECT.from(attachments) | ||
| .columns('url') | ||
| .where({ ID: { in: [...deletedAttachments] } }); | ||
| if (attachmentsToDelete.length > 0) { | ||
@@ -112,7 +116,7 @@ req.attachmentsToDelete = attachmentsToDelete; | ||
| } | ||
| } | ||
| } | ||
| async updateContentHandler(req, next) { | ||
| if (req?.data?.content) { | ||
| const response = await SELECT.from(req.target, { ID: req.data.ID }).columns("url"); | ||
| const response = await SELECT.from(req.target, { ID: req.data.ID }).columns('url'); | ||
| if (response?.url) { | ||
@@ -133,6 +137,6 @@ const Key = response.url; | ||
| const keys = { ID: req.data.ID } | ||
| scanRequest(req.target, keys) | ||
| const keys = { ID: req.data.ID }; | ||
| scanRequest(req.target, keys); | ||
| } | ||
| } else if(req?.data?.note){ | ||
| } else if (req?.data?.note) { | ||
| const key = { ID: req.data.ID }; | ||
@@ -146,11 +150,7 @@ await super.update(req.target, key, { note: req.data.note }); | ||
| registerUpdateHandlers(srv, entity, mediaElement) { | ||
| srv.before(["DELETE", "UPDATE"],entity,this.attachDeletionData.bind(this)); | ||
| srv.after(["DELETE", "UPDATE"],entity,this.deleteAttachmentsWithKeys.bind(this)); | ||
| srv.before(['DELETE', 'UPDATE'], entity, this.attachDeletionData.bind(this)); | ||
| srv.after(['DELETE', 'UPDATE'], entity, this.deleteAttachmentsWithKeys.bind(this)); | ||
| srv.prepend(() => { | ||
| if (mediaElement.drafts) { | ||
| srv.on( | ||
| "PUT", | ||
| mediaElement.drafts, | ||
| this.updateContentHandler.bind(this) | ||
| ); | ||
| srv.on('PUT', mediaElement.drafts, this.updateContentHandler.bind(this)); | ||
| } | ||
@@ -171,5 +171,5 @@ }); | ||
| async deleteInfectedAttachment(Attachments, key) { | ||
| const response = await SELECT.from(Attachments, key).columns('url') | ||
| const response = await SELECT.from(Attachments, key).columns('url'); | ||
| return await this.delete(response.url); | ||
| } | ||
| }; |
+1
-1
| { | ||
| "name": "@tklein1801/cap-js-attachments", | ||
| "description": "CAP cds-plugin providing image and attachment storing out-of-the-box.", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
39190
0.03%