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 1980069 commit 04d026c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
43 changes: 9 additions & 34 deletions server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ffmpeg from 'fluent-ffmpeg';
import fs from 'fs';
import JSZip from 'jszip';
import path from 'path';
import sharp, { FormatEnum } from 'sharp';
import sharp from 'sharp';
import { AppService } from './app.service';

interface Image {
Expand Down Expand Up @@ -41,51 +41,38 @@ export class AppController {
) {
try {
const { images, format } = payload;
console.log("Received request with format:", format);

if (format !== 'original' && !this.isValidFormat(format)) {
throw new HttpException(
'Invalid format specified',
HttpStatus.BAD_REQUEST,
);
}
console.log('Received request with format:', format);

const zip = new JSZip();

for (const image of images) {
const name = new URL(image.url).pathname.split('/').slice(-1)[0];
const name = new URL(image.url).pathname.split('/').pop() || 'image';
const response = await axios.get(image.url, {
responseType: 'arraybuffer',
});
const imageBuffer = Buffer.from(response.data);

if (format === 'original') {
const contentType = response.headers['content-type'] as string;
const contentType = response.headers['content-type'];
console.log(`Adding original image: ${name}, type: ${contentType}`);
zip.file(name, imageBuffer, { binary: true, comment: contentType });
zip.file(name, imageBuffer, { binary: true });
} else {
let sharpInstance = sharp(imageBuffer);

if (format === 'heif') {
sharpInstance = sharpInstance.heif({ quality: 80 });
} else {
sharpInstance = sharpInstance.toFormat(format as keyof FormatEnum);
}

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

const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });
console.log("ZIP file size:", zipBuffer.length);
console.log('ZIP file size:', zipBuffer.length);

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

console.log("Response sent successfully");
console.log('Response sent successfully');
return 'Images processed successfully';
} catch (error) {
console.error('Error processing images:', error);
Expand All @@ -96,18 +83,6 @@ export class AppController {
}
}

private isValidFormat(format: string): boolean {
const validFormats = [
'jpeg',
'png',
'webp',
'avif',
'heif',
'tiff',
];
return validFormats.includes(format.toLowerCase());
}

@Post('videos')
async getVideoUrl(
@Body() payload: { videos: Video[]; format: string },
Expand Down Expand Up @@ -136,4 +111,4 @@ export class AppController {
})
.run();
}
}
}
38 changes: 36 additions & 2 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
import { ImageStore } from "../../store/ImageStore";
import { Button } from "../ui/button";
import { ImageStore } from "@/store/ImageStore";
import "./App.css";
import { Images } from "./components/images";
import { ImageDownload } from "./components/images/image-download";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
import { Videos } from "./components/videos";

const App = () => {
const { selectedImages } = ImageStore();

return (
<section className="relative min-h-screen">
<div className="flex justify-between mb-4">
<Tabs defaultValue="images" className="w-full">
<TabsList className="grid w-full grid-cols-2">
<TabsTrigger value="images">Images</TabsTrigger>
<TabsTrigger value="videos">Videos</TabsTrigger>
</TabsList>
<TabsContent value="images">
<Images />
{selectedImages.length > 0 && (
<div className="fixed bottom-4 right-4 z-50">
<ImageDownload />
</div>
)}
</TabsContent>
<TabsContent value="videos">
<Videos />
</TabsContent>
</Tabs>
</div>
</section>
);
};

export default App;

0 comments on commit 04d026c

Please sign in to comment.