mirror of
https://github.com/JuLi0n21/thumbnailservice.git
synced 2026-04-19 16:00:07 +00:00
allow large files, add gif support, fix pdf naming invariance
This commit is contained in:
@@ -9,9 +9,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"image/gif"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
@@ -41,19 +43,19 @@ func generateVideoThumbnail(inputPath, outputPath string, maxWidth, maxHeight in
|
|||||||
|
|
||||||
func generatePdfThumbnail(inputPath, outputPath string, maxWidth, maxHeight int) error {
|
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()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to generate PDF thumbnail using Poppler-utils: %v", err)
|
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 {
|
if maxWidth > 0 || maxHeight > 0 {
|
||||||
return resizeImage(outputPath, outputPath, maxWidth, maxHeight)
|
return resizeImage(outputPath, outputPath, maxWidth, maxHeight)
|
||||||
}
|
}
|
||||||
@@ -111,6 +113,11 @@ func resizeImage(inputPath, outputPath string, maxWidth, maxHeight int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to save resized image as png: %v", err)
|
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:
|
default:
|
||||||
return fmt.Errorf("unsupported image type: %v", imgType)
|
return fmt.Errorf("unsupported image type: %v", imgType)
|
||||||
}
|
}
|
||||||
@@ -178,6 +185,8 @@ func (s *server) GenerateThumbnail(ctx context.Context, req *pb.ThumbnailRequest
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxMsgSize = 2147483648 // 2GB
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
listen, err := net.Listen("tcp", ":50051")
|
listen, err := net.Listen("tcp", ":50051")
|
||||||
@@ -185,7 +194,9 @@ func main() {
|
|||||||
log.Fatalf("Failed to listen: %v", err)
|
log.Fatalf("Failed to listen: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
grpcServer := grpc.NewServer()
|
grpcServer := grpc.NewServer(
|
||||||
|
grpc.MaxRecvMsgSize(maxMsgSize),
|
||||||
|
grpc.MaxSendMsgSize(maxMsgSize))
|
||||||
|
|
||||||
pb.RegisterThumbnailServiceServer(grpcServer, &server{})
|
pb.RegisterThumbnailServiceServer(grpcServer, &server{})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user