From 6a98c4d0a4c893c71d222e905ffc45fe755e674d Mon Sep 17 00:00:00 2001 From: JuLi0n21 Date: Tue, 25 Mar 2025 12:44:28 +0100 Subject: [PATCH] allow large files, add gif support, fix pdf naming invariance --- server/main.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/server/main.go b/server/main.go index 23d5732..d50af03 100644 --- a/server/main.go +++ b/server/main.go @@ -9,9 +9,11 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "strings" "time" + "image/gif" _ "image/gif" "image/jpeg" "image/png" @@ -41,19 +43,19 @@ func generateVideoThumbnail(inputPath, outputPath string, maxWidth, maxHeight in func generatePdfThumbnail(inputPath, outputPath string, maxWidth, maxHeight int) error { - cmd := exec.Command("pdftoppm", inputPath, outputPath, "-jpeg", "-f", "1", "-l", "1", "-scale-to", "200") + filename := strings.TrimSuffix(outputPath, ".jpg") + + cmd := exec.Command("pdftoppm", + inputPath, filename, "-jpeg", + "-singlefile", + "-f", "1", + "-l", "1", + "-scale-to", strconv.Itoa(maxHeight)) err := cmd.Run() if err != nil { return fmt.Errorf("failed to generate PDF thumbnail using Poppler-utils: %v", err) } - outputFileWithPage := fmt.Sprintf("%s-01.jpg", outputPath) - - err = os.Rename(outputFileWithPage, outputPath) - if err != nil { - return fmt.Errorf("failed to rename file: %v", err) - } - if maxWidth > 0 || maxHeight > 0 { return resizeImage(outputPath, outputPath, maxWidth, maxHeight) } @@ -111,6 +113,11 @@ func resizeImage(inputPath, outputPath string, maxWidth, maxHeight int) error { if err != nil { return fmt.Errorf("failed to save resized image as png: %v", err) } + case "gif": + err = gif.Encode(outFile, resizedImg, &gif.Options{}) + if err != nil { + return fmt.Errorf("failed to save resized image as png: %v", err) + } default: return fmt.Errorf("unsupported image type: %v", imgType) } @@ -178,6 +185,8 @@ func (s *server) GenerateThumbnail(ctx context.Context, req *pb.ThumbnailRequest }, nil } +const maxMsgSize = 2147483648 // 2GB + func main() { listen, err := net.Listen("tcp", ":50051") @@ -185,7 +194,9 @@ func main() { log.Fatalf("Failed to listen: %v", err) } - grpcServer := grpc.NewServer() + grpcServer := grpc.NewServer( + grpc.MaxRecvMsgSize(maxMsgSize), + grpc.MaxSendMsgSize(maxMsgSize)) pb.RegisterThumbnailServiceServer(grpcServer, &server{})