Skip to content

Commit e9854ee

Browse files
committed
Copy shared files when creating conversation from share
Adds logic to duplicate files from a shared conversation to a new conversation when created from a share. Files are renamed and metadata is updated to associate them with the new conversation.
1 parent b8e7d59 commit e9854ee

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/lib/server/conversation.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ export async function createConversationFromShare(
4747
meta: { fromShareId },
4848
});
4949

50+
// Copy files from shared conversation bucket entries to the new conversation
51+
// Shared files are stored with filenames "${sharedId}-${sha}" and metadata.conversation = sharedId
52+
// New conversation expects files to be stored under its own id prefix
53+
const newConvId = res.insertedId.toString();
54+
const sharedId = fromShareId;
55+
const files = await collections.bucket
56+
.find({ filename: { $regex: `^${sharedId}-` } })
57+
.toArray();
58+
59+
await Promise.all(
60+
files.map(
61+
(file) =>
62+
new Promise<void>((resolve, reject) => {
63+
try {
64+
const newFilename = file.filename.replace(`${sharedId}-`, `${newConvId}-`);
65+
const downloadStream = collections.bucket.openDownloadStream(file._id);
66+
const uploadStream = collections.bucket.openUploadStream(newFilename, {
67+
metadata: { ...file.metadata, conversation: newConvId },
68+
});
69+
downloadStream
70+
.on("error", reject)
71+
.pipe(uploadStream)
72+
.on("error", reject)
73+
.on("finish", () => resolve());
74+
} catch (e) {
75+
reject(e);
76+
}
77+
})
78+
)
79+
);
80+
5081
if (MetricsServer.isEnabled()) {
5182
MetricsServer.getMetrics().model.conversationsTotal.inc({ model: conversation.model });
5283
}

0 commit comments

Comments
 (0)