wow
This commit is contained in:
parent
68bb75b1c8
commit
fa35d5c305
3 changed files with 46 additions and 20 deletions
|
|
@ -18,6 +18,7 @@ type UploadResponse = {
|
|||
message: string;
|
||||
savedCount?: number;
|
||||
failedCount?: number;
|
||||
ids?: string[];
|
||||
};
|
||||
|
||||
type ExistsResponse = {
|
||||
|
|
@ -120,13 +121,13 @@ async function uploadAndCreateWithRetry(options: {
|
|||
fileName: string;
|
||||
mediaUrl: string;
|
||||
mediaIndex: number;
|
||||
createDocument: () => Promise<void>;
|
||||
createDocument: () => Promise<any>;
|
||||
}) {
|
||||
const { fileName, mediaUrl, mediaIndex, createDocument } = options;
|
||||
|
||||
const existingBefore = await MediaUpload.findOne({ s3Key: fileName, mediaIndex });
|
||||
if (existingBefore) {
|
||||
return { ok: true as const, created: false };
|
||||
return { ok: true as const, created: false, id: existingBefore._id.toString() };
|
||||
}
|
||||
|
||||
let lastError: unknown;
|
||||
|
|
@ -138,16 +139,16 @@ async function uploadAndCreateWithRetry(options: {
|
|||
|
||||
const existing = await MediaUpload.findOne({ s3Key: fileName, mediaIndex });
|
||||
if (existing) {
|
||||
return { ok: true as const, created: false };
|
||||
return { ok: true as const, created: false, id: existing._id.toString() };
|
||||
}
|
||||
|
||||
await createDocument();
|
||||
return { ok: true as const, created: true };
|
||||
const doc = await createDocument();
|
||||
return { ok: true as const, created: true, id: doc._id.toString() };
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
const existingAfterError = await MediaUpload.findOne({ s3Key: fileName, mediaIndex });
|
||||
if (existingAfterError) {
|
||||
return { ok: true as const, created: false };
|
||||
return { ok: true as const, created: false, id: existingAfterError._id.toString() };
|
||||
}
|
||||
|
||||
if (attempt < 2) {
|
||||
|
|
@ -397,7 +398,11 @@ export default new Elysia({ prefix: "/post" })
|
|||
|
||||
const existingPost = await checkExistingPostByUrl(body.url);
|
||||
if (existingPost.exists) {
|
||||
return uploadOk("이미 저장된 게시물입니다.", { savedCount: 0, failedCount: 0 });
|
||||
return uploadOk("이미 저장된 게시물입니다.", {
|
||||
savedCount: 0,
|
||||
failedCount: 0,
|
||||
ids: existingPost.documentId ? [existingPost.documentId] : [],
|
||||
});
|
||||
}
|
||||
|
||||
if (body.url.startsWith("https://www.pixiv.net/")) {
|
||||
|
|
@ -428,6 +433,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
let failedCount = 0;
|
||||
const hasExplicitSelection = body.selected.length > 0;
|
||||
|
||||
const savedIds: string[] = [];
|
||||
for (const [index, mediaUrl] of mediaUrls.entries()) {
|
||||
const isSelected = hasExplicitSelection
|
||||
? body.selected[index] === true
|
||||
|
|
@ -446,7 +452,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
mediaUrl,
|
||||
mediaIndex: index,
|
||||
createDocument: async () => {
|
||||
await MediaUpload.create({
|
||||
return await MediaUpload.create({
|
||||
type: "pixiv",
|
||||
tweet: {
|
||||
id: illustId,
|
||||
|
|
@ -477,6 +483,10 @@ export default new Elysia({ prefix: "/post" })
|
|||
continue;
|
||||
}
|
||||
|
||||
if (result.id) {
|
||||
savedIds.push(result.id);
|
||||
}
|
||||
|
||||
if (result.created) {
|
||||
await saveTags(normalizedTags);
|
||||
savedCount += 1;
|
||||
|
|
@ -523,6 +533,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
return uploadOk("업로드가 완료되었습니다.", {
|
||||
savedCount,
|
||||
failedCount,
|
||||
ids: savedIds,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[Pixiv upload aborted] requestId=${requestId} key=${uploadKey}`, error);
|
||||
|
|
@ -551,6 +562,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
if (media.length > 0) {
|
||||
const mediaUrls = media.map((m: any) => m.url);
|
||||
const hasExplicitSelection = body.selected.length > 0;
|
||||
const savedIds: string[] = [];
|
||||
for (const [index, url] of mediaUrls.entries()) {
|
||||
const isSelected = hasExplicitSelection
|
||||
? body.selected[index] === true
|
||||
|
|
@ -569,7 +581,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
mediaUrl: url,
|
||||
mediaIndex: index,
|
||||
createDocument: async () => {
|
||||
await MediaUpload.create({
|
||||
return await MediaUpload.create({
|
||||
type: "twitter",
|
||||
tweet: tweetWithoutMedia,
|
||||
mediaIndex: index,
|
||||
|
|
@ -593,6 +605,10 @@ export default new Elysia({ prefix: "/post" })
|
|||
continue;
|
||||
}
|
||||
|
||||
if (result.id) {
|
||||
savedIds.push(result.id);
|
||||
}
|
||||
|
||||
if (result.created) {
|
||||
await saveTags(normalizedTags);
|
||||
savedCount += 1;
|
||||
|
|
@ -643,6 +659,7 @@ export default new Elysia({ prefix: "/post" })
|
|||
return uploadOk("업로드가 완료되었습니다.", {
|
||||
savedCount,
|
||||
failedCount,
|
||||
ids: savedIds,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[Upload aborted] requestId=${requestId} key=${uploadKey}`, error);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue