104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package mainHandler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
pb "imageStorage/pkg/proto/imageStorage"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func (i *ImageHandler) UploadImage(ctx context.Context, req *pb.UploadMerchImageRequest) (*pb.UploadMerchImageResponse, error) {
|
|
if len(req.ImageData) == 0 {
|
|
return nil, status.Errorf(codes.InvalidArgument, "image data is empty")
|
|
}
|
|
|
|
path := i._makeMerchImagePath(req.UserUuid, req.MerchUuid)
|
|
if err := os.MkdirAll(path, 0777); err != nil {
|
|
return nil, err
|
|
}
|
|
full := filepath.Join(path, "full.jpg")
|
|
thumbnail := filepath.Join(path, "thumbnail.jpg")
|
|
|
|
fullFile, err := os.Create(full)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fullFile.Close()
|
|
|
|
thumbnailFile, err := os.Create(thumbnail)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer thumbnailFile.Close()
|
|
|
|
if err = i.converter.ConvertAndSave(req.ImageData, fullFile, thumbnailFile); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := &pb.UploadMerchImageResponse{
|
|
FullImage: i._makeMerchImageURL(req.UserUuid, req.MerchUuid, full),
|
|
Thumbnail: i._makeMerchImageURL(req.UserUuid, req.MerchUuid, thumbnail),
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (i *ImageHandler) GetImage(ctx context.Context, req *pb.GetImageRequest) (*pb.GetImageResponse, error) {
|
|
path := i._makeMerchImagePath(req.UserUuid, req.MerchUuid)
|
|
unk := status.Error(codes.InvalidArgument, "unknown image type")
|
|
|
|
switch req.ImgType {
|
|
case pb.ImageType_UNKNOWN:
|
|
return nil, unk
|
|
case pb.ImageType_FULL:
|
|
return &pb.GetImageResponse{
|
|
Url: fmt.Sprintf("%s/%s", i.domain, filepath.Join(path, "full.jpg")),
|
|
Etag: "",
|
|
}, nil
|
|
case pb.ImageType_THUMBNAIL:
|
|
return &pb.GetImageResponse{
|
|
Url: fmt.Sprintf("%s/%s", i.domain, filepath.Join(path, "thumbnail.jpg")),
|
|
Etag: "",
|
|
}, nil
|
|
|
|
default:
|
|
return nil, unk
|
|
}
|
|
}
|
|
|
|
func (i *ImageHandler) DeleteImage(ctx context.Context, req *pb.DeleteImageRequest) (*emptypb.Empty, error) {
|
|
path := i._makeMerchImagePath(req.UserUuid, req.MerchUuid)
|
|
|
|
entries, err := os.ReadDir(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if !entry.IsDir() {
|
|
filePath := filepath.Join(path, entry.Name())
|
|
if err := os.Remove(filePath); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
func (i *ImageHandler) _makeMerchImagePath(u, m string) string {
|
|
return fmt.Sprintf("%s/merchImages/%s/%s", i.volume, u, m)
|
|
}
|
|
|
|
func (i *ImageHandler) _makeMerchImageURL(u, m, n string) string {
|
|
d := strings.TrimSuffix(i.domain, "/")
|
|
return fmt.Sprintf("%s/merchImages/%s/%s/%s", d, u, m, n)
|
|
}
|