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 276d658 commit 4f1bc60
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
22 changes: 20 additions & 2 deletions server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ export class AppController {
const { images, format } = payload;
console.log('Received request with format:', format);

if (format === 'original' && images.length === 1) {
// Handle single original image download
const image = images[0];
const response = await axios.get(image.url, {
responseType: 'arraybuffer',
});
const imageBuffer = Buffer.from(response.data);
const contentType = response.headers['content-type'];
const name = new URL(image.url).pathname.split('/').pop() || 'image';

reply
.header('Content-Type', contentType)
.header('Content-Disposition', `attachment; filename=${name}`)
.send(imageBuffer);

console.log('Single original image sent successfully');
return 'Image processed successfully';
}

const zip = new JSZip();

for (const image of images) {
Expand All @@ -52,7 +71,6 @@ export class AppController {
});
const imageBuffer = Buffer.from(response.data);

// Log the content type of the image
const contentType = response.headers['content-type'];
console.log(`Processing image: ${name}, Content-Type: ${contentType}`);

Expand Down Expand Up @@ -115,4 +133,4 @@ export class AppController {
})
.run();
}
}
}
2 changes: 0 additions & 2 deletions web/src/components/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export const Images: React.FC = () => {
};
}, [handleTabChange]);

console.log(images);

if (loading) {
return (
<div className="flex justify-center items-center h-screen">
Expand Down
24 changes: 18 additions & 6 deletions web/src/components/images/image-download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ export const ImageDownload: React.FC = () => {
console.log("Response type:", response.data.type);

const contentDisposition = response.headers["content-disposition"];
const filename = contentDisposition
let filename = contentDisposition
? contentDisposition.split("filename=")[1].replace(/"/g, "")
: "images.zip";
: "download";

const blob = new Blob([response.data], { type: "application/zip" });
const blob = new Blob([response.data], { type: response.data.type });

// Handle different content types
if (response.data.type === "application/zip") {
filename = filename || "images.zip";
} else {
// For single file downloads (e.g., when format is 'original' and only one image)
const extension =
format === "original" ? images[0].url.split(".").pop() : format;
filename = `${filename || "image"}.${extension}`;
}

const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
Expand Down Expand Up @@ -73,9 +83,11 @@ export const ImageDownload: React.FC = () => {
<SelectValue placeholder="Select format" />
</SelectTrigger>
<SelectContent>
<SelectItem value="original">original</SelectItem>
{IMAGE_FORMATS.map((fmt) => (
<SelectItem key={fmt} value={fmt}>
<SelectItem key="original" value="original">
original
</SelectItem>
{IMAGE_FORMATS.map((fmt, index) => (
<SelectItem key={`${fmt}-${index}`} value={fmt}>
{fmt.toUpperCase()}
</SelectItem>
))}
Expand Down

0 comments on commit 4f1bc60

Please sign in to comment.