Skip to content

Commit

Permalink
Merge pull request #27 from lemoncloud-capstone/BE_day
Browse files Browse the repository at this point in the history
feat: modify to send image URLs along with Blobs
  • Loading branch information
Yeeun411 authored May 14, 2024
2 parents 7c9b3d6 + c4bae58 commit 8c6e0be
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions apps/server/src/repositories/img.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QueryCommand, QueryCommandInput, UpdateCommand } from '@aws-sdk/lib-dynamodb';
import axios from 'axios';

import { ddbDocumentClient } from './index';

Expand All @@ -22,16 +23,40 @@ export class ImgRepository {
};

const { Items, LastEvaluatedKey } = await this.ddbClient.send(new QueryCommand(queryParams));
console.log('Items', Items);
return {
lastEvaluatedKey: LastEvaluatedKey ? JSON.stringify(LastEvaluatedKey) : null,
workers: Items.map((item: any) => item.workers).flat(),
img: Items.map((item: any) => ({
imgURL: item.skey,
status: item.status,
labelPoint: item.labelPoints,
})),
};
try {
// 비동기로 모든 이미지 URL을 Blob 형태로 가져오기
const imgPromises = Items.map(async (item: any) => {
try {
const response = await axios.get(item.skey, { responseType: 'arraybuffer' });
const blob = Buffer.from(response.data, 'binary').toString('base64');
return {
imgURL: item.skey,
status: item.status,
labelPoint: item.labelPoints,
blob: blob,
};
} catch (error) {
console.error(`Error fetching image from ${item.skey}:`, error.message);
return {
imgURL: item.skey,
status: item.status,
labelPoint: item.labelPoints,
blob: null,
};
}
});

// 모든 이미지를 Blob 형태로 변환할 때까지 기다리기
const img = await Promise.all(imgPromises);

return {
lastEvaluatedKey: LastEvaluatedKey ? JSON.stringify(LastEvaluatedKey) : null,
img: img,
};
} catch (error) {
console.error('Error fetching images:', error);
throw error;
}
}

public async updateImageStatus(title: string, imageURL: string, status: string, labelPoints): Promise<void> {
Expand Down

0 comments on commit 8c6e0be

Please sign in to comment.