Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Sep 24, 2024
1 parent 054925c commit 703f12a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 5 additions & 16 deletions server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,18 @@ export class AppController {
);
}

const imagePath = path.join(path.resolve(), 'images');
fs.mkdirSync(imagePath, { recursive: true });

const zip = new JSZip();

for (const image of images) {
const name = new URL(image.url).pathname.split('/').slice(-1)[0];
const imageType = image.headers
?.find((header) => header.name.toLowerCase() === 'content-type')
?.value.replace('image/', '');

const response = await axios.get(image.url, {
responseType: 'arraybuffer',
});
const imageBuffer = Buffer.from(response.data, 'binary');
const imageBuffer = Buffer.from(response.data);

if (format === 'original') {
zip.file(name, imageBuffer);
const contentType = response.headers['content-type'];
zip.file(name, imageBuffer, { binary: true });
} else {
let sharpInstance = sharp(imageBuffer);

Expand All @@ -74,23 +68,18 @@ export class AppController {
}

const processedImage = await sharpInstance.toBuffer();
zip.file(`${name}.${format}`, processedImage);
zip.file(`${name}.${format}`, processedImage, { binary: true });
}
}

const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });

const zipFileName = path.join(imagePath, `images.zip`);
fs.writeFileSync(zipFileName, zipBuffer);

reply
.header('Content-Type', 'application/zip')
.header('Content-Disposition', `attachment; filename=images.zip`)
.send(zipBuffer);

fs.unlinkSync(zipFileName);

return zipFileName;
return 'Images processed successfully';
} catch (error) {
console.error('Error processing images:', error);
throw new HttpException(
Expand Down
2 changes: 1 addition & 1 deletion web/src/store/ImageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ImageStore = {

export const ImageStore = create<ImageStore>()(
immer<ImageStore>((set, get) => ({
format: "orginal" as ImageFormat,
format: "original",
setFormat: (format) => set((state) => ({ ...state, format })),

currentTabId: null,
Expand Down

0 comments on commit 703f12a

Please sign in to comment.