2021-07-29 22:04:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/nfnt/resize"
|
|
|
|
)
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
func newMinioClient() (*minio.Client, error) {
|
|
|
|
endpoint := config.S3Endpoint
|
|
|
|
accessKeyID := config.S3AccessKey
|
|
|
|
secretKeyID := config.S3SecretKey
|
|
|
|
useSSL := true
|
|
|
|
|
|
|
|
//Initialize Minio
|
|
|
|
minioCLient, err := minio.New(endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretKeyID, ""),
|
|
|
|
Secure: useSSL,
|
|
|
|
Region: config.S3Region,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return minioCLient, nil
|
|
|
|
}
|
2021-07-30 14:30:15 +00:00
|
|
|
|
2022-12-01 22:05:59 +00:00
|
|
|
// Upload image through guichet server.
|
2023-09-25 13:35:54 +00:00
|
|
|
func uploadProfilePicture(w http.ResponseWriter, r *http.Request, user *LoggedUser) (string, error) {
|
2021-07-29 22:04:17 +00:00
|
|
|
file, _, err := r.FormFile("image")
|
|
|
|
|
|
|
|
if err == http.ErrMissingFile {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", nil
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
2021-08-16 13:30:14 +00:00
|
|
|
|
2021-07-30 14:30:15 +00:00
|
|
|
err = checkImage(file)
|
2021-07-29 22:04:17 +00:00
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
buffFull := bytes.NewBuffer([]byte{})
|
|
|
|
buffThumb := bytes.NewBuffer([]byte{})
|
|
|
|
err = resizePicture(file, buffFull, buffThumb)
|
2021-07-29 22:04:17 +00:00
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:30:15 +00:00
|
|
|
mc, err := newMinioClient()
|
|
|
|
if err != nil || mc == nil {
|
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
// If a previous profile picture existed, delete it
|
|
|
|
// (don't care about errors)
|
2023-09-25 13:35:54 +00:00
|
|
|
if nameConsul := user.Entry.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE); nameConsul != "" {
|
2021-08-16 13:30:14 +00:00
|
|
|
mc.RemoveObject(context.Background(), config.S3Bucket, nameConsul, minio.RemoveObjectOptions{})
|
|
|
|
mc.RemoveObject(context.Background(), config.S3Bucket, nameConsul+"-thumb", minio.RemoveObjectOptions{})
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
// Generate new random name for picture
|
|
|
|
nameFull := uuid.New().String()
|
|
|
|
nameThumb := nameFull + "-thumb"
|
|
|
|
|
|
|
|
_, err = mc.PutObject(context.Background(), config.S3Bucket, nameThumb, buffThumb, int64(buffThumb.Len()), minio.PutObjectOptions{
|
2021-07-29 22:04:17 +00:00
|
|
|
ContentType: "image/jpeg",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
_, err = mc.PutObject(context.Background(), config.S3Bucket, nameFull, buffFull, int64(buffFull.Len()), minio.PutObjectOptions{
|
2021-07-29 22:04:17 +00:00
|
|
|
ContentType: "image/jpeg",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return "", err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
return nameFull, nil
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:30:15 +00:00
|
|
|
func checkImage(file multipart.File) error {
|
2021-07-29 22:04:17 +00:00
|
|
|
buff := make([]byte, 512) //Detect read only the first 512 bytes
|
|
|
|
_, err := file.Read(buff)
|
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
return err
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
file.Seek(0, 0)
|
|
|
|
|
|
|
|
fileType := http.DetectContentType(buff)
|
|
|
|
fileType = strings.Split(fileType, "/")[0]
|
2021-08-16 13:30:14 +00:00
|
|
|
if fileType != "image" {
|
2021-07-30 14:30:15 +00:00
|
|
|
return errors.New("bad type")
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
return nil
|
2021-07-29 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
func resizePicture(file multipart.File, buffFull, buffThumb *bytes.Buffer) error {
|
2021-07-29 22:04:17 +00:00
|
|
|
file.Seek(0, 0)
|
2021-08-16 13:30:14 +00:00
|
|
|
picture, _, err := image.Decode(file)
|
2021-07-29 22:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
thumbnail := resize.Thumbnail(90, 90, picture, resize.Lanczos3)
|
|
|
|
picture = resize.Thumbnail(480, 480, picture, resize.Lanczos3)
|
|
|
|
|
|
|
|
err = jpeg.Encode(buffFull, picture, &jpeg.Options{
|
2021-07-29 22:04:17 +00:00
|
|
|
Quality: 95,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
err = jpeg.Encode(buffThumb, thumbnail, &jpeg.Options{
|
|
|
|
Quality: 100,
|
2021-07-29 22:04:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
func handleDownloadPicture(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := mux.Vars(r)["name"]
|
|
|
|
|
2023-09-25 13:35:54 +00:00
|
|
|
// Get user
|
|
|
|
user := RequireUserHtml(w, r)
|
|
|
|
if user == nil {
|
2021-07-29 22:04:17 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-16 13:30:14 +00:00
|
|
|
|
2021-07-29 22:04:17 +00:00
|
|
|
//Get the object after connect MC
|
2021-07-30 14:30:15 +00:00
|
|
|
mc, err := newMinioClient()
|
2021-07-29 22:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "MinioClient: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-08-16 13:30:14 +00:00
|
|
|
|
|
|
|
obj, err := mc.GetObject(context.Background(), "bottin-pictures", name, minio.GetObjectOptions{})
|
2021-07-29 22:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "MinioClient: GetObject: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer obj.Close()
|
2021-08-16 13:30:14 +00:00
|
|
|
|
2021-07-29 22:04:17 +00:00
|
|
|
objStat, err := obj.Stat()
|
|
|
|
if err != nil {
|
2021-07-30 14:30:15 +00:00
|
|
|
http.Error(w, "MiniObjet: "+err.Error(), http.StatusInternalServerError)
|
2021-07-29 22:04:17 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-16 13:30:14 +00:00
|
|
|
|
2021-07-29 22:04:17 +00:00
|
|
|
//Send JSON through xhttp
|
|
|
|
w.Header().Set("Content-Type", objStat.ContentType)
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(int(objStat.Size)))
|
2021-07-30 14:30:15 +00:00
|
|
|
//Copy obj in w
|
|
|
|
writting, err := io.Copy(w, obj)
|
2021-08-16 13:30:14 +00:00
|
|
|
|
2021-07-30 14:30:15 +00:00
|
|
|
if writting != objStat.Size || err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("WriteBody: %s, bytes wrote %d on %d", err.Error(), writting, objStat.Size), http.StatusInternalServerError)
|
2021-07-29 22:04:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|