Skip to content

Commit 064e25f

Browse files
committed
update webapp.js to include multiple attachments
1 parent 3344c53 commit 064e25f

File tree

1 file changed

+46
-18
lines changed
  • onepassword_sdks/demo-vault-backup-webapp

1 file changed

+46
-18
lines changed

onepassword_sdks/demo-vault-backup-webapp/webapp.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ class OnePasswordSDK {
578578
}
579579
}
580580

581+
// Create an item in a vault, handling Documents and additional files
581582
// Create an item in a vault, handling Documents and additional files
582583
async createItem(vaultId, item) {
583584
if (!this.client) await this.initializeClient();
@@ -602,15 +603,14 @@ class OnePasswordSDK {
602603
contentType: 'application/octet-stream'
603604
}
604605
};
605-
createdItem = await this.client.items.create(newItem);
606-
console.log(`Document "${item.title}" created with ID: ${createdItem.id}`);
607-
backupLog.push(`[INFO] Document "${item.title}" created with ID: ${createdItem.id}`);
608606

609-
// Attach any additional files for Document items (e.g., for "DeleteVaults")
607+
// Include additional files in client.items.create
610608
if (item.files && item.files.length > 0) {
611609
console.log(`Attaching ${item.files.length} additional files to Document "${item.title}"...`);
612610
backupLog.push(`[INFO] Attaching ${item.files.length} additional files to Document "${item.title}"`);
613-
for (const file of item.files) {
611+
newItem.files = [];
612+
const fileSectionIds = new Set();
613+
for (const [index, file] of item.files.entries()) {
614614
if (!file.content) {
615615
console.error(`Skipping file "${file.name}" for "${item.title}": content is missing`);
616616
backupLog.push(`[ERROR] Skipping file "${file.name}" for "${item.title}": content is missing`);
@@ -620,17 +620,32 @@ class OnePasswordSDK {
620620
const fileContent = new Uint8Array(fileBuffer.buffer, fileBuffer.byteOffset, fileBuffer.byteLength);
621621
console.log(`Attaching file "${file.name}", size: ${fileContent.length}`);
622622
backupLog.push(`[INFO] Attaching file "${file.name}", size: ${fileContent.length}`);
623-
await this.client.items.files.attach(createdItem, {
623+
const fileSectionId = file.sectionId || "restored-section";
624+
const fileFieldId = file.fieldId || `restored-file-${Date.now()}-${index}`;
625+
newItem.files.push({
624626
name: file.name,
625627
content: fileContent,
626-
sectionId: file.sectionId || "restored-section",
627-
fieldId: file.fieldId || `restored-file-${Date.now()}`,
628+
sectionId: fileSectionId,
629+
fieldId: fileFieldId,
628630
contentType: 'application/octet-stream'
629631
});
632+
fileSectionIds.add(fileSectionId);
630633
console.log(`File "${file.name}" attached to "${item.title}"`);
631634
backupLog.push(`[INFO] File "${file.name}" attached to "${item.title}"`);
632635
}
636+
637+
// Ensure file sectionIds are in newItem.sections
638+
newItem.sections = newItem.sections || [];
639+
for (const sectionId of fileSectionIds) {
640+
if (!newItem.sections.some(section => section.id === sectionId)) {
641+
newItem.sections.push({ id: sectionId, title: sectionId === "restored-section" ? "Restored Section" : sectionId });
642+
}
643+
}
633644
}
645+
646+
createdItem = await this.client.items.create(newItem);
647+
console.log(`Document "${item.title}" created with ID: ${createdItem.id}`);
648+
backupLog.push(`[INFO] Document "${item.title}" created with ID: ${createdItem.id}`);
634649
} else {
635650
// Handle non-Document items (Logins, Secure Notes, etc.)
636651
const sectionIdsFromFields = new Set();
@@ -657,17 +672,14 @@ class OnePasswordSDK {
657672
websites: item.websites || [],
658673
notes: item.notes || ""
659674
};
660-
console.log(`Creating item "${item.title}"...`);
661-
backupLog.push(`[INFO] Creating item "${item.title}"`);
662-
createdItem = await this.client.items.create(newItem);
663-
console.log(`Item "${item.title}" created with ID: ${createdItem.id}`);
664-
backupLog.push(`[INFO] Item "${item.title}" created with ID: ${createdItem.id}`);
665675

666-
// Attach any files for non-Document items
676+
// Include files in client.items.create
667677
if (item.files && item.files.length > 0) {
668678
console.log(`Attaching ${item.files.length} files to "${item.title}"...`);
669679
backupLog.push(`[INFO] Attaching ${item.files.length} files to "${item.title}"`);
670-
for (const file of item.files) {
680+
newItem.files = [];
681+
const fileSectionIds = new Set();
682+
for (const [index, file] of item.files.entries()) {
671683
if (!file.content) {
672684
console.error(`Skipping file "${file.name}" for "${item.title}": content is missing`);
673685
backupLog.push(`[ERROR] Skipping file "${file.name}" for "${item.title}": content is missing`);
@@ -677,17 +689,33 @@ class OnePasswordSDK {
677689
const fileContent = new Uint8Array(fileBuffer.buffer, fileBuffer.byteOffset, fileBuffer.byteLength);
678690
console.log(`Attaching file "${file.name}", size: ${fileContent.length}`);
679691
backupLog.push(`[INFO] Attaching file "${file.name}", size: ${fileContent.length}`);
680-
await this.client.items.files.attach(createdItem, {
692+
const fileSectionId = file.sectionId || "restored-section";
693+
const fileFieldId = file.fieldId || `restored-file-${Date.now()}-${index}`;
694+
newItem.files.push({
681695
name: file.name,
682696
content: fileContent,
683-
sectionId: file.sectionId || "restored-section",
684-
fieldId: file.fieldId || `restored-file-${Date.now()}`,
697+
sectionId: fileSectionId,
698+
fieldId: fileFieldId,
685699
contentType: 'application/octet-stream'
686700
});
701+
fileSectionIds.add(fileSectionId);
687702
console.log(`File "${file.name}" attached to "${item.title}"`);
688703
backupLog.push(`[INFO] File "${file.name}" attached to "${item.title}"`);
689704
}
705+
706+
// Ensure file sectionIds are in newItem.sections
707+
for (const sectionId of fileSectionIds) {
708+
if (!newItem.sections.some(section => section.id === sectionId)) {
709+
newItem.sections.push({ id: sectionId, title: sectionId === "restored-section" ? "Restored Section" : sectionId });
710+
}
711+
}
690712
}
713+
714+
console.log(`Creating item "${item.title}"...`);
715+
backupLog.push(`[INFO] Creating item "${item.title}"`);
716+
createdItem = await this.client.items.create(newItem);
717+
console.log(`Item "${item.title}" created with ID: ${createdItem.id}`);
718+
backupLog.push(`[INFO] Item "${item.title}" created with ID: ${createdItem.id}`);
691719
}
692720
} catch (error) {
693721
console.error(`Item creation failed for "${item.title}": ${error.message}`);

0 commit comments

Comments
 (0)