commit d640f5007fd88a4a5d44ccd4391be77fa0c972b3 Author: JuLi0n21 Date: Mon Mar 24 21:45:12 2025 +0100 init diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..6eb6cbc --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,44 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: ./server + push: true + tags: juli0n21/thumbnails:latest + + - name: Logout from Docker Hub + run: docker logout diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d64a70a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +client/testdata +client/thumbnail \ No newline at end of file diff --git a/MAKEFILE b/MAKEFILE new file mode 100644 index 0000000..b413678 --- /dev/null +++ b/MAKEFILE @@ -0,0 +1,9 @@ +# Makefile + +# Define the 'proto' target +proto: + # Generate Go code for the client + protoc --go_out=./client --go-grpc_out=./client ./thumbnail.proto + + # Generate Go code for the server + protoc --go_out=./server --go-grpc_out=./server ./thumbnail.proto diff --git a/README.md b/README.md new file mode 100644 index 0000000..c00b9c2 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +### Small docker containe to generate preview images for diffrent filetypes + +# Usage: +- generate client code from the `thumbnail.proto` file, make the request with the files ... profit? + +> [!WARNING] +> their are no plans to maintain or extend this + +#### Dependencys + +check the [Dockerfile](./server/Dockerfile) and [go.mod](./server/go.mod) \ No newline at end of file diff --git a/client/go.mod b/client/go.mod new file mode 100644 index 0000000..c8f564f --- /dev/null +++ b/client/go.mod @@ -0,0 +1,15 @@ +module thumbnailclient + +go 1.24.1 + +require ( + google.golang.org/grpc v1.71.0 + google.golang.org/protobuf v1.36.6 +) + +require ( + golang.org/x/net v0.34.0 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.21.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect +) diff --git a/client/go.sum b/client/go.sum new file mode 100644 index 0000000..25d5ce5 --- /dev/null +++ b/client/go.sum @@ -0,0 +1,34 @@ +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY= +go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI= +go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ= +go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE= +go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A= +go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU= +go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk= +go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w= +go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k= +go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:OxYkA3wjPsZyBylwymxSHa7ViiW1Sml4ToBrncvFehI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:+2Yz8+CLJbIfL9z73EW45avw8Lmge3xVElCP9zEKi50= +google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg= +google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec= +google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= +google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= diff --git a/client/main.go b/client/main.go new file mode 100644 index 0000000..0da5781 --- /dev/null +++ b/client/main.go @@ -0,0 +1,91 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "path" + "path/filepath" + "strings" + "time" + + pb "thumbnailclient/proto" + + "google.golang.org/grpc" +) + +func main() { + conn, err := grpc.NewClient("localhost:50051", grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + log.Fatalf("Failed to connect to server: %v", err) + } + defer conn.Close() + + type thingy struct { + Type pb.FileType + Path string + } + client := pb.NewThumbnailServiceClient(conn) + filePath := []thingy{ + {pb.FileType_IMAGE, "testdata/image-sample.png"}, + {pb.FileType_PDF, "testdata/pdf-sample.pdf"}, + {pb.FileType_VIDEO, "testdata/video-sample.webm"}} + + for _, f := range filePath { + newFunction(f.Path, f.Type, client) + } + +} + +func newFunction(filePath string, ftype pb.FileType, client pb.ThumbnailServiceClient) { + fileContent, err := os.ReadFile(filePath) + if err != nil { + log.Fatalf("Error reading file: %v", err) + } + + req := &pb.ThumbnailRequest{ + FileContent: fileContent, + FileType: ftype, + MaxHeight: 150, + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + resp, err := client.GenerateThumbnail(ctx, req) + if err != nil { + log.Fatalf("Error calling GenerateThumbnail: %v", err) + } + + fmt.Printf("Response: %s\n", resp.Message) + if len(resp.ThumbnailContent) > 0 { + err := saveThumbnailToFile(resp.ThumbnailContent, filePath) + if err != nil { + log.Fatalf("Error saving thumbnail to file: %v", err) + } + fmt.Println("Thumbnail saved successfully.") + } else { + log.Println("No thumbnail content received.") + } +} + +// Function to save the thumbnail content to a file in the 'thumbnail/' directory +func saveThumbnailToFile(thumbnailContent []byte, filePath string) error { + // Ensure the "thumbnail" directory exists + err := os.MkdirAll("thumbnail", os.ModePerm) + if err != nil { + return fmt.Errorf("failed to create directory: %v", err) + } + + // Create the file where the thumbnail will be saved + baseName := filepath.Base(filePath) + fileName := strings.TrimSuffix(baseName, filepath.Ext(baseName)) + + err = os.WriteFile(path.Join("thumbnail", fileName)+".jpg", thumbnailContent, 0644) + if err != nil { + return fmt.Errorf("failed to save thumbnail to file: %v", err) + } + + return nil +} diff --git a/client/proto/thumbnail.pb.go b/client/proto/thumbnail.pb.go new file mode 100644 index 0000000..d6e4e74 --- /dev/null +++ b/client/proto/thumbnail.pb.go @@ -0,0 +1,274 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.6 +// protoc v3.21.12 +// source: thumbnail.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Enum for the file type +type FileType int32 + +const ( + FileType_FILE_TYPE_UNSPECIFIED FileType = 0 // Default value for unspecified file type + FileType_IMAGE FileType = 1 // Image file + FileType_VIDEO FileType = 2 // Video file + FileType_PDF FileType = 3 // PDF file +) + +// Enum value maps for FileType. +var ( + FileType_name = map[int32]string{ + 0: "FILE_TYPE_UNSPECIFIED", + 1: "IMAGE", + 2: "VIDEO", + 3: "PDF", + } + FileType_value = map[string]int32{ + "FILE_TYPE_UNSPECIFIED": 0, + "IMAGE": 1, + "VIDEO": 2, + "PDF": 3, + } +) + +func (x FileType) Enum() *FileType { + p := new(FileType) + *p = x + return p +} + +func (x FileType) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (FileType) Descriptor() protoreflect.EnumDescriptor { + return file_thumbnail_proto_enumTypes[0].Descriptor() +} + +func (FileType) Type() protoreflect.EnumType { + return &file_thumbnail_proto_enumTypes[0] +} + +func (x FileType) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use FileType.Descriptor instead. +func (FileType) EnumDescriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{0} +} + +// Request message for generating thumbnails +type ThumbnailRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + FileContent []byte `protobuf:"bytes,1,opt,name=file_content,json=fileContent,proto3" json:"file_content,omitempty"` // File content as bytes + FileType FileType `protobuf:"varint,2,opt,name=file_type,json=fileType,proto3,enum=thumbnail_service.FileType" json:"file_type,omitempty"` // File type (image, video, pdf) + MaxWidth int32 `protobuf:"varint,3,opt,name=max_width,json=maxWidth,proto3" json:"max_width,omitempty"` // Optional max width for resizing (0 means no limit) + MaxHeight int32 `protobuf:"varint,4,opt,name=max_height,json=maxHeight,proto3" json:"max_height,omitempty"` // Optional max height for resizing (0 means no limit) + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ThumbnailRequest) Reset() { + *x = ThumbnailRequest{} + mi := &file_thumbnail_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ThumbnailRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ThumbnailRequest) ProtoMessage() {} + +func (x *ThumbnailRequest) ProtoReflect() protoreflect.Message { + mi := &file_thumbnail_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ThumbnailRequest.ProtoReflect.Descriptor instead. +func (*ThumbnailRequest) Descriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{0} +} + +func (x *ThumbnailRequest) GetFileContent() []byte { + if x != nil { + return x.FileContent + } + return nil +} + +func (x *ThumbnailRequest) GetFileType() FileType { + if x != nil { + return x.FileType + } + return FileType_FILE_TYPE_UNSPECIFIED +} + +func (x *ThumbnailRequest) GetMaxWidth() int32 { + if x != nil { + return x.MaxWidth + } + return 0 +} + +func (x *ThumbnailRequest) GetMaxHeight() int32 { + if x != nil { + return x.MaxHeight + } + return 0 +} + +// Response message for the thumbnail generation +type ThumbnailResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` // Message indicating success or failure + ThumbnailContent []byte `protobuf:"bytes,2,opt,name=thumbnail_content,json=thumbnailContent,proto3" json:"thumbnail_content,omitempty"` // Thumbnail content as bytes + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ThumbnailResponse) Reset() { + *x = ThumbnailResponse{} + mi := &file_thumbnail_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ThumbnailResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ThumbnailResponse) ProtoMessage() {} + +func (x *ThumbnailResponse) ProtoReflect() protoreflect.Message { + mi := &file_thumbnail_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ThumbnailResponse.ProtoReflect.Descriptor instead. +func (*ThumbnailResponse) Descriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{1} +} + +func (x *ThumbnailResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +func (x *ThumbnailResponse) GetThumbnailContent() []byte { + if x != nil { + return x.ThumbnailContent + } + return nil +} + +var File_thumbnail_proto protoreflect.FileDescriptor + +const file_thumbnail_proto_rawDesc = "" + + "\n" + + "\x0fthumbnail.proto\x12\x11thumbnail_service\"\xab\x01\n" + + "\x10ThumbnailRequest\x12!\n" + + "\ffile_content\x18\x01 \x01(\fR\vfileContent\x128\n" + + "\tfile_type\x18\x02 \x01(\x0e2\x1b.thumbnail_service.FileTypeR\bfileType\x12\x1b\n" + + "\tmax_width\x18\x03 \x01(\x05R\bmaxWidth\x12\x1d\n" + + "\n" + + "max_height\x18\x04 \x01(\x05R\tmaxHeight\"Z\n" + + "\x11ThumbnailResponse\x12\x18\n" + + "\amessage\x18\x01 \x01(\tR\amessage\x12+\n" + + "\x11thumbnail_content\x18\x02 \x01(\fR\x10thumbnailContent*D\n" + + "\bFileType\x12\x19\n" + + "\x15FILE_TYPE_UNSPECIFIED\x10\x00\x12\t\n" + + "\x05IMAGE\x10\x01\x12\t\n" + + "\x05VIDEO\x10\x02\x12\a\n" + + "\x03PDF\x10\x032r\n" + + "\x10ThumbnailService\x12^\n" + + "\x11GenerateThumbnail\x12#.thumbnail_service.ThumbnailRequest\x1a$.thumbnail_service.ThumbnailResponseB\tZ\a./protob\x06proto3" + +var ( + file_thumbnail_proto_rawDescOnce sync.Once + file_thumbnail_proto_rawDescData []byte +) + +func file_thumbnail_proto_rawDescGZIP() []byte { + file_thumbnail_proto_rawDescOnce.Do(func() { + file_thumbnail_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_thumbnail_proto_rawDesc), len(file_thumbnail_proto_rawDesc))) + }) + return file_thumbnail_proto_rawDescData +} + +var file_thumbnail_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_thumbnail_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_thumbnail_proto_goTypes = []any{ + (FileType)(0), // 0: thumbnail_service.FileType + (*ThumbnailRequest)(nil), // 1: thumbnail_service.ThumbnailRequest + (*ThumbnailResponse)(nil), // 2: thumbnail_service.ThumbnailResponse +} +var file_thumbnail_proto_depIdxs = []int32{ + 0, // 0: thumbnail_service.ThumbnailRequest.file_type:type_name -> thumbnail_service.FileType + 1, // 1: thumbnail_service.ThumbnailService.GenerateThumbnail:input_type -> thumbnail_service.ThumbnailRequest + 2, // 2: thumbnail_service.ThumbnailService.GenerateThumbnail:output_type -> thumbnail_service.ThumbnailResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_thumbnail_proto_init() } +func file_thumbnail_proto_init() { + if File_thumbnail_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_thumbnail_proto_rawDesc), len(file_thumbnail_proto_rawDesc)), + NumEnums: 1, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_thumbnail_proto_goTypes, + DependencyIndexes: file_thumbnail_proto_depIdxs, + EnumInfos: file_thumbnail_proto_enumTypes, + MessageInfos: file_thumbnail_proto_msgTypes, + }.Build() + File_thumbnail_proto = out.File + file_thumbnail_proto_goTypes = nil + file_thumbnail_proto_depIdxs = nil +} diff --git a/client/proto/thumbnail_grpc.pb.go b/client/proto/thumbnail_grpc.pb.go new file mode 100644 index 0000000..7da6426 --- /dev/null +++ b/client/proto/thumbnail_grpc.pb.go @@ -0,0 +1,125 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v3.21.12 +// source: thumbnail.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + ThumbnailService_GenerateThumbnail_FullMethodName = "/thumbnail_service.ThumbnailService/GenerateThumbnail" +) + +// ThumbnailServiceClient is the client API for ThumbnailService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +// +// Service definition +type ThumbnailServiceClient interface { + GenerateThumbnail(ctx context.Context, in *ThumbnailRequest, opts ...grpc.CallOption) (*ThumbnailResponse, error) +} + +type thumbnailServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewThumbnailServiceClient(cc grpc.ClientConnInterface) ThumbnailServiceClient { + return &thumbnailServiceClient{cc} +} + +func (c *thumbnailServiceClient) GenerateThumbnail(ctx context.Context, in *ThumbnailRequest, opts ...grpc.CallOption) (*ThumbnailResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ThumbnailResponse) + err := c.cc.Invoke(ctx, ThumbnailService_GenerateThumbnail_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ThumbnailServiceServer is the server API for ThumbnailService service. +// All implementations must embed UnimplementedThumbnailServiceServer +// for forward compatibility. +// +// Service definition +type ThumbnailServiceServer interface { + GenerateThumbnail(context.Context, *ThumbnailRequest) (*ThumbnailResponse, error) + mustEmbedUnimplementedThumbnailServiceServer() +} + +// UnimplementedThumbnailServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedThumbnailServiceServer struct{} + +func (UnimplementedThumbnailServiceServer) GenerateThumbnail(context.Context, *ThumbnailRequest) (*ThumbnailResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GenerateThumbnail not implemented") +} +func (UnimplementedThumbnailServiceServer) mustEmbedUnimplementedThumbnailServiceServer() {} +func (UnimplementedThumbnailServiceServer) testEmbeddedByValue() {} + +// UnsafeThumbnailServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ThumbnailServiceServer will +// result in compilation errors. +type UnsafeThumbnailServiceServer interface { + mustEmbedUnimplementedThumbnailServiceServer() +} + +func RegisterThumbnailServiceServer(s grpc.ServiceRegistrar, srv ThumbnailServiceServer) { + // If the following call pancis, it indicates UnimplementedThumbnailServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&ThumbnailService_ServiceDesc, srv) +} + +func _ThumbnailService_GenerateThumbnail_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ThumbnailRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ThumbnailServiceServer).GenerateThumbnail(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: ThumbnailService_GenerateThumbnail_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ThumbnailServiceServer).GenerateThumbnail(ctx, req.(*ThumbnailRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ThumbnailService_ServiceDesc is the grpc.ServiceDesc for ThumbnailService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ThumbnailService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "thumbnail_service.ThumbnailService", + HandlerType: (*ThumbnailServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GenerateThumbnail", + Handler: _ThumbnailService_GenerateThumbnail_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "thumbnail.proto", +} diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..02d6a37 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:1.24.1 + +WORKDIR /app + +RUN apt-get update && apt-get install -y \ + imagemagick \ + ffmpeg \ + poppler-utils && \ + rm -rf /var/lib/apt/lists/* + +COPY . . + +EXPOSE 50051 + +RUN go mod tidy && go build -o thumbnail-service + +CMD ["./thumbnail-service"] \ No newline at end of file diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..90b6f5a --- /dev/null +++ b/server/go.mod @@ -0,0 +1,16 @@ +module github.com/JuLi0n21/thumbnail_service + +go 1.24.1 + +require ( + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + google.golang.org/grpc v1.71.0 + google.golang.org/protobuf v1.36.6 +) + +require ( + golang.org/x/net v0.37.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect +) diff --git a/server/go.sum b/server/go.sum new file mode 100644 index 0000000..1402567 --- /dev/null +++ b/server/go.sum @@ -0,0 +1,46 @@ +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY= +go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI= +go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ= +go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE= +go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A= +go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU= +go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk= +go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w= +go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k= +go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:OxYkA3wjPsZyBylwymxSHa7ViiW1Sml4ToBrncvFehI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:+2Yz8+CLJbIfL9z73EW45avw8Lmge3xVElCP9zEKi50= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 h1:iK2jbkWL86DXjEx0qiHcRE9dE4/Ahua5k6V8OWFb//c= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I= +google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg= +google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec= +google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM= +google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= +google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..e545a14 --- /dev/null +++ b/server/main.go @@ -0,0 +1,210 @@ +package main + +import ( + "context" + "fmt" + "image" + "log" + "net" + "os" + "os/exec" + "strings" + + _ "image/gif" + "image/jpeg" + _ "image/jpeg" + "image/png" + _ "image/png" + + pb "github.com/JuLi0n21/thumbnail_service/proto" + "github.com/nfnt/resize" + "google.golang.org/grpc" +) + +// Helper function to generate video thumbnails using ffmpeg +func generateVideoThumbnail(inputPath, outputPath string, maxWidth, maxHeight int) error { + cmd := exec.Command("ffmpeg", "-i", inputPath, "-vf", "thumbnail", "-frames:v", "1", outputPath) + var stderr strings.Builder + cmd.Stderr = &stderr + + err := cmd.Run() + + if err != nil { + return fmt.Errorf("failed to generate video thumbnail using FFmpeg: %v. FFmpeg stderr: %s", err, stderr.String()) + } + + if maxWidth > 0 || maxHeight > 0 { + return resizeImage(outputPath, outputPath, maxWidth, maxHeight) + } + return nil +} + +// Helper function to generate PDF thumbnails using poppler-utils (pdftoppm) +func generatePdfThumbnail(inputPath, outputPath string, maxWidth, maxHeight int) error { + // Command for Poppler-utils to generate a thumbnail from the first page of a PDF file + cmd := exec.Command("pdftoppm", inputPath, outputPath, "-jpeg", "-f", "1", "-l", "1", "-scale-to", "200") + 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) // pdftoppm output file with page number suffix + + // Rename the file + err = os.Rename(outputFileWithPage, outputPath) + if err != nil { + return fmt.Errorf("failed to rename file: %v", err) + } + + // Resize the generated image if maxWidth or maxHeight is provided + if maxWidth > 0 || maxHeight > 0 { + return resizeImage(outputPath, outputPath, maxWidth, maxHeight) + } + + return nil +} + +func resizeImage(inputPath, outputPath string, maxWidth, maxHeight int) error { + // Open the input image file + file, err := os.Open(inputPath) + if err != nil { + return fmt.Errorf("failed to open image file: %v", err) + } + defer file.Close() + + // Decode the image + img, imgType, err := image.Decode(file) + if err != nil { + return fmt.Errorf("failed to decode image: %v", err) + } + + // Calculate the new dimensions, preserving the aspect ratio + var newWidth, newHeight int + if maxWidth > 0 && maxHeight > 0 { + // Resize with both width and height limit + newWidth = maxWidth + newHeight = maxHeight + } else if maxWidth > 0 { + // Resize based on width + newWidth = maxWidth + newHeight = int(float64(img.Bounds().Dy()) * float64(maxWidth) / float64(img.Bounds().Dx())) + } else if maxHeight > 0 { + // Resize based on height + newHeight = maxHeight + newWidth = int(float64(img.Bounds().Dx()) * float64(maxHeight) / float64(img.Bounds().Dy())) + } else { + // No resizing needed + newWidth = img.Bounds().Dx() + newHeight = img.Bounds().Dy() + } + + // Resize the image using the calculated dimensions + resizedImg := resize.Resize(uint(newWidth), uint(newHeight), img, resize.Lanczos3) + + // Create the output file + outFile, err := os.Create(outputPath) + if err != nil { + return fmt.Errorf("failed to create output file: %v", err) + } + defer outFile.Close() + switch imgType { + case "jpeg": + err = jpeg.Encode(outFile, resizedImg, nil) + if err != nil { + return fmt.Errorf("failed to save resized image as jpeg: %v", err) + } + case "png": + err = png.Encode(outFile, resizedImg) + if err != nil { + return fmt.Errorf("failed to save resized image as png: %v", err) + } + default: + return fmt.Errorf("unsupported image type: %v", imgType) + } + + return nil +} + +type server struct { + pb.UnimplementedThumbnailServiceServer +} + +func (s *server) GenerateThumbnail(ctx context.Context, req *pb.ThumbnailRequest) (*pb.ThumbnailResponse, error) { + // Create a temporary file to store the uploaded content + tempFile, err := os.CreateTemp("", "upload-*") + if err != nil { + return nil, fmt.Errorf("failed to create temporary file: %v", err) + } + defer os.Remove(tempFile.Name()) // Cleanup temporary file + + // Write the content from the request to the temporary file + err = os.WriteFile(tempFile.Name(), req.FileContent, 0644) + if err != nil { + return nil, fmt.Errorf("failed to write content to file: %v", err) + } + + // Generate thumbnail and save it to disk based on file type + var outputPath string + + // Check file type using enum + switch req.FileType { + case pb.FileType_IMAGE: + // Image file, use ImageMagick or other Go logic to create a thumbnail + outputPath = "thumbnails/image-thumbnail.jpg" + err = resizeImage(tempFile.Name(), outputPath, int(req.MaxWidth), int(req.MaxHeight)) + if err != nil { + return nil, err + } + + case pb.FileType_VIDEO: + // Video file, use FFmpeg to create a thumbnail + outputPath = "thumbnails/video-thumbnail.jpg" // Video thumbnails are typically saved as JPG + err = generateVideoThumbnail(tempFile.Name(), outputPath, int(req.MaxWidth), int(req.MaxHeight)) + if err != nil { + return nil, err + } + + case pb.FileType_PDF: + // PDF file, use Poppler-utils to create a thumbnail + outputPath = "thumbnails/pdf-thumbnail.jpg" // PDF thumbnails are typically saved as JPG + err = generatePdfThumbnail(tempFile.Name(), outputPath, int(req.MaxWidth), int(req.MaxHeight)) + if err != nil { + return nil, err + } + + default: + return nil, fmt.Errorf("unsupported file type: %v", req.FileType) + } + + // Read the generated thumbnail back into memory to send it as bytes + thumbnailContent, err := os.ReadFile(outputPath) + if err != nil { + return nil, fmt.Errorf("failed to read generated thumbnail: %v", err) + } + + // Return the response with the thumbnail bytes and output path + return &pb.ThumbnailResponse{ + Message: "Thumbnail generated successfully", + ThumbnailContent: thumbnailContent, // Send the thumbnail as bytes + }, nil +} + +func main() { + // Set up a listener on port 50051 + listen, err := net.Listen("tcp", ":50051") + if err != nil { + log.Fatalf("Failed to listen: %v", err) + } + + // Create a gRPC server + grpcServer := grpc.NewServer() + + // Register the server + pb.RegisterThumbnailServiceServer(grpcServer, &server{}) + + // Start serving requests + log.Println("Server started on port 50051") + if err := grpcServer.Serve(listen); err != nil { + log.Fatalf("Failed to serve: %v", err) + } +} diff --git a/server/proto/thumbnail.pb.go b/server/proto/thumbnail.pb.go new file mode 100644 index 0000000..d6e4e74 --- /dev/null +++ b/server/proto/thumbnail.pb.go @@ -0,0 +1,274 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.6 +// protoc v3.21.12 +// source: thumbnail.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Enum for the file type +type FileType int32 + +const ( + FileType_FILE_TYPE_UNSPECIFIED FileType = 0 // Default value for unspecified file type + FileType_IMAGE FileType = 1 // Image file + FileType_VIDEO FileType = 2 // Video file + FileType_PDF FileType = 3 // PDF file +) + +// Enum value maps for FileType. +var ( + FileType_name = map[int32]string{ + 0: "FILE_TYPE_UNSPECIFIED", + 1: "IMAGE", + 2: "VIDEO", + 3: "PDF", + } + FileType_value = map[string]int32{ + "FILE_TYPE_UNSPECIFIED": 0, + "IMAGE": 1, + "VIDEO": 2, + "PDF": 3, + } +) + +func (x FileType) Enum() *FileType { + p := new(FileType) + *p = x + return p +} + +func (x FileType) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (FileType) Descriptor() protoreflect.EnumDescriptor { + return file_thumbnail_proto_enumTypes[0].Descriptor() +} + +func (FileType) Type() protoreflect.EnumType { + return &file_thumbnail_proto_enumTypes[0] +} + +func (x FileType) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use FileType.Descriptor instead. +func (FileType) EnumDescriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{0} +} + +// Request message for generating thumbnails +type ThumbnailRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + FileContent []byte `protobuf:"bytes,1,opt,name=file_content,json=fileContent,proto3" json:"file_content,omitempty"` // File content as bytes + FileType FileType `protobuf:"varint,2,opt,name=file_type,json=fileType,proto3,enum=thumbnail_service.FileType" json:"file_type,omitempty"` // File type (image, video, pdf) + MaxWidth int32 `protobuf:"varint,3,opt,name=max_width,json=maxWidth,proto3" json:"max_width,omitempty"` // Optional max width for resizing (0 means no limit) + MaxHeight int32 `protobuf:"varint,4,opt,name=max_height,json=maxHeight,proto3" json:"max_height,omitempty"` // Optional max height for resizing (0 means no limit) + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ThumbnailRequest) Reset() { + *x = ThumbnailRequest{} + mi := &file_thumbnail_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ThumbnailRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ThumbnailRequest) ProtoMessage() {} + +func (x *ThumbnailRequest) ProtoReflect() protoreflect.Message { + mi := &file_thumbnail_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ThumbnailRequest.ProtoReflect.Descriptor instead. +func (*ThumbnailRequest) Descriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{0} +} + +func (x *ThumbnailRequest) GetFileContent() []byte { + if x != nil { + return x.FileContent + } + return nil +} + +func (x *ThumbnailRequest) GetFileType() FileType { + if x != nil { + return x.FileType + } + return FileType_FILE_TYPE_UNSPECIFIED +} + +func (x *ThumbnailRequest) GetMaxWidth() int32 { + if x != nil { + return x.MaxWidth + } + return 0 +} + +func (x *ThumbnailRequest) GetMaxHeight() int32 { + if x != nil { + return x.MaxHeight + } + return 0 +} + +// Response message for the thumbnail generation +type ThumbnailResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` // Message indicating success or failure + ThumbnailContent []byte `protobuf:"bytes,2,opt,name=thumbnail_content,json=thumbnailContent,proto3" json:"thumbnail_content,omitempty"` // Thumbnail content as bytes + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ThumbnailResponse) Reset() { + *x = ThumbnailResponse{} + mi := &file_thumbnail_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ThumbnailResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ThumbnailResponse) ProtoMessage() {} + +func (x *ThumbnailResponse) ProtoReflect() protoreflect.Message { + mi := &file_thumbnail_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ThumbnailResponse.ProtoReflect.Descriptor instead. +func (*ThumbnailResponse) Descriptor() ([]byte, []int) { + return file_thumbnail_proto_rawDescGZIP(), []int{1} +} + +func (x *ThumbnailResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +func (x *ThumbnailResponse) GetThumbnailContent() []byte { + if x != nil { + return x.ThumbnailContent + } + return nil +} + +var File_thumbnail_proto protoreflect.FileDescriptor + +const file_thumbnail_proto_rawDesc = "" + + "\n" + + "\x0fthumbnail.proto\x12\x11thumbnail_service\"\xab\x01\n" + + "\x10ThumbnailRequest\x12!\n" + + "\ffile_content\x18\x01 \x01(\fR\vfileContent\x128\n" + + "\tfile_type\x18\x02 \x01(\x0e2\x1b.thumbnail_service.FileTypeR\bfileType\x12\x1b\n" + + "\tmax_width\x18\x03 \x01(\x05R\bmaxWidth\x12\x1d\n" + + "\n" + + "max_height\x18\x04 \x01(\x05R\tmaxHeight\"Z\n" + + "\x11ThumbnailResponse\x12\x18\n" + + "\amessage\x18\x01 \x01(\tR\amessage\x12+\n" + + "\x11thumbnail_content\x18\x02 \x01(\fR\x10thumbnailContent*D\n" + + "\bFileType\x12\x19\n" + + "\x15FILE_TYPE_UNSPECIFIED\x10\x00\x12\t\n" + + "\x05IMAGE\x10\x01\x12\t\n" + + "\x05VIDEO\x10\x02\x12\a\n" + + "\x03PDF\x10\x032r\n" + + "\x10ThumbnailService\x12^\n" + + "\x11GenerateThumbnail\x12#.thumbnail_service.ThumbnailRequest\x1a$.thumbnail_service.ThumbnailResponseB\tZ\a./protob\x06proto3" + +var ( + file_thumbnail_proto_rawDescOnce sync.Once + file_thumbnail_proto_rawDescData []byte +) + +func file_thumbnail_proto_rawDescGZIP() []byte { + file_thumbnail_proto_rawDescOnce.Do(func() { + file_thumbnail_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_thumbnail_proto_rawDesc), len(file_thumbnail_proto_rawDesc))) + }) + return file_thumbnail_proto_rawDescData +} + +var file_thumbnail_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_thumbnail_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_thumbnail_proto_goTypes = []any{ + (FileType)(0), // 0: thumbnail_service.FileType + (*ThumbnailRequest)(nil), // 1: thumbnail_service.ThumbnailRequest + (*ThumbnailResponse)(nil), // 2: thumbnail_service.ThumbnailResponse +} +var file_thumbnail_proto_depIdxs = []int32{ + 0, // 0: thumbnail_service.ThumbnailRequest.file_type:type_name -> thumbnail_service.FileType + 1, // 1: thumbnail_service.ThumbnailService.GenerateThumbnail:input_type -> thumbnail_service.ThumbnailRequest + 2, // 2: thumbnail_service.ThumbnailService.GenerateThumbnail:output_type -> thumbnail_service.ThumbnailResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_thumbnail_proto_init() } +func file_thumbnail_proto_init() { + if File_thumbnail_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_thumbnail_proto_rawDesc), len(file_thumbnail_proto_rawDesc)), + NumEnums: 1, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_thumbnail_proto_goTypes, + DependencyIndexes: file_thumbnail_proto_depIdxs, + EnumInfos: file_thumbnail_proto_enumTypes, + MessageInfos: file_thumbnail_proto_msgTypes, + }.Build() + File_thumbnail_proto = out.File + file_thumbnail_proto_goTypes = nil + file_thumbnail_proto_depIdxs = nil +} diff --git a/server/proto/thumbnail_grpc.pb.go b/server/proto/thumbnail_grpc.pb.go new file mode 100644 index 0000000..7da6426 --- /dev/null +++ b/server/proto/thumbnail_grpc.pb.go @@ -0,0 +1,125 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v3.21.12 +// source: thumbnail.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + ThumbnailService_GenerateThumbnail_FullMethodName = "/thumbnail_service.ThumbnailService/GenerateThumbnail" +) + +// ThumbnailServiceClient is the client API for ThumbnailService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +// +// Service definition +type ThumbnailServiceClient interface { + GenerateThumbnail(ctx context.Context, in *ThumbnailRequest, opts ...grpc.CallOption) (*ThumbnailResponse, error) +} + +type thumbnailServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewThumbnailServiceClient(cc grpc.ClientConnInterface) ThumbnailServiceClient { + return &thumbnailServiceClient{cc} +} + +func (c *thumbnailServiceClient) GenerateThumbnail(ctx context.Context, in *ThumbnailRequest, opts ...grpc.CallOption) (*ThumbnailResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ThumbnailResponse) + err := c.cc.Invoke(ctx, ThumbnailService_GenerateThumbnail_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ThumbnailServiceServer is the server API for ThumbnailService service. +// All implementations must embed UnimplementedThumbnailServiceServer +// for forward compatibility. +// +// Service definition +type ThumbnailServiceServer interface { + GenerateThumbnail(context.Context, *ThumbnailRequest) (*ThumbnailResponse, error) + mustEmbedUnimplementedThumbnailServiceServer() +} + +// UnimplementedThumbnailServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedThumbnailServiceServer struct{} + +func (UnimplementedThumbnailServiceServer) GenerateThumbnail(context.Context, *ThumbnailRequest) (*ThumbnailResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GenerateThumbnail not implemented") +} +func (UnimplementedThumbnailServiceServer) mustEmbedUnimplementedThumbnailServiceServer() {} +func (UnimplementedThumbnailServiceServer) testEmbeddedByValue() {} + +// UnsafeThumbnailServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ThumbnailServiceServer will +// result in compilation errors. +type UnsafeThumbnailServiceServer interface { + mustEmbedUnimplementedThumbnailServiceServer() +} + +func RegisterThumbnailServiceServer(s grpc.ServiceRegistrar, srv ThumbnailServiceServer) { + // If the following call pancis, it indicates UnimplementedThumbnailServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&ThumbnailService_ServiceDesc, srv) +} + +func _ThumbnailService_GenerateThumbnail_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ThumbnailRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ThumbnailServiceServer).GenerateThumbnail(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: ThumbnailService_GenerateThumbnail_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ThumbnailServiceServer).GenerateThumbnail(ctx, req.(*ThumbnailRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ThumbnailService_ServiceDesc is the grpc.ServiceDesc for ThumbnailService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ThumbnailService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "thumbnail_service.ThumbnailService", + HandlerType: (*ThumbnailServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GenerateThumbnail", + Handler: _ThumbnailService_GenerateThumbnail_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "thumbnail.proto", +} diff --git a/server/thumbnails/image-thumbnail.jpg b/server/thumbnails/image-thumbnail.jpg new file mode 100644 index 0000000..b6b8740 Binary files /dev/null and b/server/thumbnails/image-thumbnail.jpg differ diff --git a/server/thumbnails/pdf-thumbnail.jpg b/server/thumbnails/pdf-thumbnail.jpg new file mode 100644 index 0000000..3ec27e7 Binary files /dev/null and b/server/thumbnails/pdf-thumbnail.jpg differ diff --git a/server/thumbnails/video-thumbnail.jpg b/server/thumbnails/video-thumbnail.jpg new file mode 100644 index 0000000..c97a1c7 Binary files /dev/null and b/server/thumbnails/video-thumbnail.jpg differ diff --git a/thumbnail.proto b/thumbnail.proto new file mode 100644 index 0000000..69cc98c --- /dev/null +++ b/thumbnail.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +package thumbnail_service; + +option go_package = "./proto"; + +// Enum for the file type +enum FileType { + FILE_TYPE_UNSPECIFIED = 0; // Default value for unspecified file type + IMAGE = 1; // Image file + VIDEO = 2; // Video file + PDF = 3; // PDF file +} + +// Service definition +service ThumbnailService { + rpc GenerateThumbnail(ThumbnailRequest) returns (ThumbnailResponse); +} + +// Request message for generating thumbnails +message ThumbnailRequest { + bytes file_content = 1; // File content as bytes + FileType file_type = 2; // File type (image, video, pdf) + int32 max_width = 3; // Optional max width for resizing (0 means no limit) + int32 max_height = 4; // Optional max height for resizing (0 means no limit) +} + +// Response message for the thumbnail generation +message ThumbnailResponse { + string message = 1; // Message indicating success or failure + bytes thumbnail_content = 2; // Thumbnail content as bytes +}