Skip to content

Commit

Permalink
Version 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Aug 20, 2024
1 parent ca65bad commit 61c2152
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<img src="https://takecontrolsoft.eu/assets/img/takecontrolsoft-logo-green.png" alt="Sync Device by Take Control - software & infrastructure" width="25%">

## 1.0.3 Release notes (2024-08-20)

### Enhancements
* Implemented API `/img` that returns thumbnails streams.
* Implemented API `/delete-all` that deletes all the files for the device and user from the server.


## 1.0.2 Release notes (2024-08-18)

### Enhancements
Expand Down
5 changes: 2 additions & 3 deletions server/impl/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ func DeleteAllHandler(w http.ResponseWriter, r *http.Request) {

err := os.RemoveAll(userDirName)
if err != nil {
if utils.RenderIfError(err, w, http.StatusInternalServerError) {
return
}
utils.RenderError(w, err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down
56 changes: 41 additions & 15 deletions server/impl/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package impl

import (
"encoding/json"
"image"
"image/png"
"net/http"
"os"
"path/filepath"

"github.com/go-errors/errors"
Expand All @@ -34,7 +34,6 @@ type fileData struct {

func GetImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var files = make([]string, 0)
var result fileData
if err := json.NewDecoder(r.Body).Decode(&result); err != nil {
utils.RenderError(w, errors.Errorf("$Required json input {UserData: { User: '', DeviceId: ''}, File: ''}"), http.StatusBadRequest)
Expand All @@ -44,25 +43,52 @@ func GetImageHandler(w http.ResponseWriter, r *http.Request) {
deviceId := result.UserData.DeviceId
file := result.File
userDirName := filepath.Join(config.UploadDirectory, userName, deviceId)
filePath := filepath.Join(userDirName, file)
src, err := utils.GetImageFromFilePath(filePath)
var thumbnailPath = filepath.Join(userDirName, "Thumbnails", file)
if _, err := os.Stat(thumbnailPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
thumbnailPath, err = BuildThumbnail(userName, deviceId, file)
if err != nil {
utils.RenderError(w, err, http.StatusInternalServerError)
return
}
} else {
utils.RenderError(w, err, http.StatusInternalServerError)
return
}
}
src, err := utils.GetImageFromFilePath(thumbnailPath)

if err != nil {
utils.RenderError(w, err, http.StatusBadRequest)
utils.RenderError(w, err, http.StatusInternalServerError)
return
}
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/2, src.Bounds().Max.Y/2))

// Encode to `output`:
png.Encode(w, dst)
png.Encode(w, src)

w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
}
}

if err := json.NewEncoder(w).Encode(files); err != nil {
if utils.RenderIfError(err, w, http.StatusInternalServerError) {
return
}
}
func BuildThumbnail(userName string, deviceId string, file string) (string, error) {
userDirName := filepath.Join(config.UploadDirectory, userName, deviceId)
thumbnailPath := filepath.Join(userDirName, "Thumbnails", file)
filePath := filepath.Join(userDirName, file)
src, err := utils.GetImageFromFilePath(filePath)
if err != nil {
return "", err
}

rgba_src := utils.ImageToRGBA(src)
resized := utils.ResizeImage(rgba_src, 90)
err = os.MkdirAll(filepath.Dir(thumbnailPath), os.ModePerm)
if err != nil {
return "", err
}
f, err := os.Create(thumbnailPath)
if err != nil {
return "", err
}
defer f.Close()
png.Encode(f, resized)
return thumbnailPath, nil
}
1 change: 1 addition & 0 deletions server/impl/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
userNameEncoded := r.Header.Get("user")

var name []byte
if err := json.Unmarshal([]byte(userNameEncoded), &name); err != nil {
utils.RenderError(w, err, http.StatusInternalServerError)
Expand Down
15 changes: 4 additions & 11 deletions server/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func JsonReaderFactory(in interface{}) (io.Reader, error) {
return buf, nil
}

func ResizeImage(img image.RGBA, height int) image.RGBA {
func ResizeImage(img *image.RGBA, height int) *image.RGBA {
if height < 50 {
return img
}
Expand All @@ -91,17 +91,15 @@ func ResizeImage(img image.RGBA, height int) image.RGBA {
resizedImage.Set(x, y, imgColor)
}
}
return *resizedImage
return resizedImage
}

func ImageToRGBA(src image.Image) *image.RGBA {

// No conversion needed if image is an *image.RGBA.
if dst, ok := src.(*image.RGBA); ok {
return dst
}

// Use the image/draw package to convert to *image.RGBA.
b := src.Bounds()
dst := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(dst, dst.Bounds(), src, b.Min, draw.Src)
Expand All @@ -114,13 +112,8 @@ func GetImageFromFilePath(filePath string) (image.Image, error) {
logger.Error(err)
}

config, info, err := image.DecodeConfig(reader)
if err != nil {
logger.Error(err)
return nil, err
}
print(config.Height)
print(info)
reader.Seek(0, 0)

m, _, err := image.Decode(reader)
if err != nil {
logger.Error(err)
Expand Down

0 comments on commit 61c2152

Please sign in to comment.