Skip to content

Commit

Permalink
attachmentCreationOnUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmeet29 committed Feb 5, 2025
1 parent 304c923 commit a98603f
Show file tree
Hide file tree
Showing 5 changed files with 533 additions and 512 deletions.
33 changes: 29 additions & 4 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ async function getFolderIdByPath(req, credentials, token, attachments) {
}

async function createFolder(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const upID = attachments.keys.up_.keys[0].$generatedFieldName;
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("propertyValue[0]", req.data[upID]);
formData.append("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:folder");
formData.append("succinct", "true");
Expand All @@ -104,7 +103,6 @@ async function createAttachment(
data,
credentials,
token,
attachments,
parentId
) {
const { repositoryId } = getConfigurations();
Expand Down Expand Up @@ -171,6 +169,32 @@ async function deleteFolderWithAttachments(credentials, token, parentId) {
return response;
}

async function getAttachment(uri, token, objectId) {
const { repositoryId } = getConfigurations();
const getAttachmentURL =
uri
+ "browser/"
+ repositoryId
+ "/root?"
+ "cmisselector=object&objectId="
+ objectId
+ "&succinct=true";

const config = {
headers: { Authorization: `Bearer ${token}` },
};
try {
return await axios.get(getAttachmentURL, config);
} catch (error) {
let statusText = "An Error Occurred";
if (error.response?.statusText) {
statusText = error.response.statusText;
}
console.log(statusText);
return null;
}
}

async function renameAttachment(
modifiedAttachment,
credentials,
Expand Down Expand Up @@ -215,6 +239,7 @@ module.exports = {
createAttachment,
deleteAttachmentsOfFolder,
deleteFolderWithAttachments,
getAttachment,
readAttachment,
renameAttachment
};
44 changes: 32 additions & 12 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,74 @@ async function getDraftAttachments(attachments, req, repositoryId) {
.where(conditions)
}

async function getDraftAttachmentsForUpID(attachments, req, repositoryId) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const conditions = {
[up_]: req.data[up_],
repositoryId: repositoryId
};
return await SELECT("filename", "mimeType", "content", "url", "ID", "HasActiveEntity")
.from(attachments)
.where(conditions)
}

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

async function updateAttachmentInDraft(req, data) {
const up_ = req.target.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
return await UPDATE(req.target)
.set({ folderId: data.folderId, url: data.url, status: "Clean" })
.where({ [idValue]: req.data[idValue] });
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
return await SELECT.from(attachments)
.columns("url")
.where({ ID: { in: [...deletedAttachments] } });
}

async function getExistingAttachments(attachmentIDs, attachments) {
return await SELECT("filename", "url", "ID","folderId")
return await SELECT("filename", "url", "ID", "folderId")
.from(attachments)
.where({ ID: { in: [...attachmentIDs] }});
}

async function setRepositoryId(attachments, repositoryId) {
if(attachments){
if(attachments) {
let nullAttachments = await SELECT()
.from(attachments)
.where({ repositoryId: null });

if (!nullAttachments || nullAttachments.length === 0) {
return;
}
if (!nullAttachments || nullAttachments.length === 0) {
return;
}

for (let attachment of nullAttachments) {
await UPDATE(attachments)
.set({ repositoryId: repositoryId })
.where({ ID: attachment.ID });
}
for (let attachment of nullAttachments) {
await UPDATE(attachments)
.set({ repositoryId: repositoryId })
.where({ ID: attachment.ID });
}
}
}


module.exports = {
getDraftAttachments,
getDraftAttachmentsForUpID,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity,
updateAttachmentInDraft,
getExistingAttachments,
setRepositoryId
};
Loading

0 comments on commit a98603f

Please sign in to comment.