-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDMEXT354/Capability to attach same document to different entities #12
Merged
+696
−218
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,62 @@ 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) { | ||
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 +120,7 @@ async function createAttachment(data, credentials, token, attachments) { | |
return response; | ||
} | ||
|
||
async function deleteAttachment(credentials, token, objectId, attachments) { | ||
async function deleteAttachmentOrFolder(credentials, token, objectId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls rename it to deleteAttachmentsOfFolder There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
const { repositoryId } = getConfigurations(); | ||
const documentDeleteURL = | ||
credentials.uri + "browser/" + repositoryId + "/root"; | ||
|
@@ -84,6 +140,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 +172,10 @@ const updateServerRequest = async ( | |
}; | ||
|
||
module.exports = { | ||
getFolderIdByPath, | ||
createFolder, | ||
createAttachment, | ||
deleteAttachment, | ||
deleteAttachmentOrFolder, | ||
deleteFolderWithAttachments, | ||
readAttachment, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add console.log(error) here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done