Skip to content

Commit

Permalink
Merge pull request #12 from cap-js/SDMEXT354/CreateFolder
Browse files Browse the repository at this point in the history
SDMEXT354/Capability to attach same document to different entities
  • Loading branch information
yashmeet29 authored May 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 0d22321 + 4b8a7ce commit f231f0f
Showing 9 changed files with 696 additions and 218 deletions.
4 changes: 4 additions & 0 deletions index.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using { Attachments} from '@cap-js/attachments';
extend aspect Attachments with {
folderId : String @title: 'Folder ID';
};
101 changes: 90 additions & 11 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
@@ -4,27 +4,33 @@ const FormData = require("form-data");

async function readAttachment(Key, token, credentials) {
try {
const document = await readDocument(Key, token, credentials.uri)
return document
const document = await readDocument(Key, token, credentials.uri);
return document;
} catch (error) {
throw new Error(error);
}
}

async function readDocument(Key, token, uri){
async function readDocument(Key, token, uri) {
const { repositoryId } = getConfigurations();
const documentReadURL = uri+ "browser/" + repositoryId + "/root?objectID=" + Key + "&cmisselector=content";
const documentReadURL =
uri +
"browser/" +
repositoryId +
"/root?objectID=" +
Key +
"&cmisselector=content";
const config = {
headers: {Authorization: `Bearer ${token}`},
responseType: 'arraybuffer'
headers: { Authorization: `Bearer ${token}` },
responseType: "arraybuffer",
};

try {
const response = await axios.get(documentReadURL, config);
const responseBuffer = Buffer.from(response.data, 'binary');
const responseBuffer = Buffer.from(response.data, "binary");
return responseBuffer;
} catch (error) {
let statusText = "An Error Occurred";
let statusText = "An Error Occurred";
if (error.response && error.response.statusText) {
statusText = error.response.statusText;
}
@@ -33,12 +39,67 @@ async function readDocument(Key, token, uri){
}
}

async function createAttachment(data, credentials, token, attachments) {
async function getFolderIdByPath(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const { repositoryId } = getConfigurations();
const getFolderByPathURL =
credentials.uri +
"browser/" +
repositoryId +
"/root/" +
req.data[idValue] +
"?cmisselector=object";
const config = {
headers: { Authorization: `Bearer ${token}` },
};
try {
const response = await axios.get(getFolderByPathURL, config);
return response.data.properties["cmis:objectId"].value;
} catch (error) {
let statusText = "An Error Occurred";
if (error.response && error.response.statusText) {
statusText = error.response.statusText;
}
console.log(statusText);
return null;
}
}

async function createFolder(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const { repositoryId } = getConfigurations();
const folderCreateURL = credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "createFolder");
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyValue[0]", req.data[idValue]);
formData.append("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:folder");
formData.append("succinct", "true");

let headers = formData.getHeaders();
headers["Authorization"] = "Bearer " + token;
const config = {
headers: headers,
};
return await updateServerRequest(folderCreateURL, formData, config);
}

async function createAttachment(
data,
credentials,
token,
attachments,
parentId
) {
const { repositoryId } = getConfigurations();
const documentCreateURL =
credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "createDocument");
formData.append("objectId", parentId);
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyValue[0]", data.filename);
formData.append("propertyId[1]", "cmis:objectTypeId");
@@ -64,7 +125,7 @@ async function createAttachment(data, credentials, token, attachments) {
return response;
}

async function deleteAttachment(credentials, token, objectId, attachments) {
async function deleteAttachmentsOfFolder(credentials, token, objectId) {
const { repositoryId } = getConfigurations();
const documentDeleteURL =
credentials.uri + "browser/" + repositoryId + "/root";
@@ -84,6 +145,21 @@ async function deleteAttachment(credentials, token, objectId, attachments) {
return response;
}

async function deleteFolderWithAttachments(credentials, token, parentId) {
const { repositoryId } = getConfigurations();
const folderDeleteURL = credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "deleteTree");
formData.append("objectId", parentId);
let headers = formData.getHeaders();
headers["Authorization"] = "Bearer " + token;
const config = {
headers: headers,
};
const response = await updateServerRequest(folderDeleteURL, formData, config);
return response;
}

const updateServerRequest = async (
url,
formData,
@@ -101,7 +177,10 @@ const updateServerRequest = async (
};

module.exports = {
getFolderIdByPath,
createFolder,
createAttachment,
deleteAttachment,
deleteAttachmentsOfFolder,
deleteFolderWithAttachments,
readAttachment,
};
14 changes: 8 additions & 6 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
@@ -14,10 +14,12 @@ async function getDraftAttachments(attachments, req) {
.and({ HasActiveEntity: { "<>": 1 } });
}

async function getDuplicateAttachments(fileNames, attachments) {
return await SELECT.distinct(["filename"])
.from(attachments)
.where({ filename: { in: fileNames } });
async function getFolderIdForEntity(attachments, req) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
return await SELECT.from(attachments)
.columns("folderId")
.where({ [up_]: req.data[idValue] });
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
@@ -27,7 +29,7 @@ async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
}
module.exports = {
getDraftAttachments,
getDuplicateAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments
getURLFromAttachments,
getFolderIdForEntity,
};
Loading

0 comments on commit f231f0f

Please sign in to comment.