Commit 245904e7 authored by fredix's avatar fredix

store thumbnail in mongodb

parent e2b5f026
package controllers
import (
"bufio"
"bytes"
"encoding/gob"
"fmt"
"io"
......@@ -8,7 +10,6 @@ import (
"net/http"
"os"
"strconv"
// "time"
"framagit.org/fredix/gopilot/models"
......@@ -165,58 +166,109 @@ func (mc MediaController) PlayBackMedia(c echo.Context) error {
func (mc MediaController) createThumbnail(file_id string) string {
thumbnail := "public/cache/thumbnail"
thumbnail_path := "public/cache/thumbnail"
if _, err := os.Stat(thumbnail + "/" + file_id + ".png"); err == nil {
// path/to/whatever exists
if _, err := os.Stat(thumbnail_path + "/" + file_id + ".png"); err == nil {
return "/thumbnail/" + file_id + ".png"
} else if os.IsNotExist(err) {
// path/to/whatever does *not* exist
fmt.Println("thumbnail no exist in fs")
// Stub an user to be populated from the body
//m := new(models.Media)
file, err := mc.session.DB(mc.db).GridFS("fs").OpenId(bson.ObjectIdHex(file_id))
if err != nil {
log.Println("error on search file on gridfs : ", err)
}
tmpFile, err := os.OpenFile(thumbnail+"/"+file_id, os.O_RDWR|os.O_CREATE, 0644)
// Check for errors when opening the file
if err != nil {
log.Println("error on open or create file : ", err)
}
_, err = io.Copy(tmpFile, file)
if err != nil {
log.Println(err)
}
file.Close()
tmpFile.Close()
fmt.Println("tmpFile name : ", tmpFile.Name())
src, err := imaging.Open(thumbnail + "/" + file_id)
media := models.Media{}
err = mc.session.DB(mc.db).C("medias").Find(bson.M{"gridfsid": file_id}).One(&media)
if err != nil {
log.Println("failed to open image: %v", err)
log.Println("error on search file : ", err)
return ""
}
// Resize the cropped image to width = 200px preserving the aspect ratio.
src = imaging.Resize(src, 100, 0, imaging.Lanczos)
// Save the resulting image as JPEG.
err = imaging.Save(src, thumbnail+"/"+file_id+".png")
if err != nil {
//log.Printlnf("failed to save image: %v", err)
fmt.Println("failed to save image: %v", err)
if len(media.Thumbnail.Data) == 0 {
fmt.Println("media thumbnail is empty")
file, err := mc.session.DB(mc.db).GridFS("fs").OpenId(bson.ObjectIdHex(file_id))
if err != nil {
log.Println("error on search file on gridfs : ", err)
return ""
}
defer file.Close()
tmpFile, err := os.OpenFile(thumbnail_path+"/"+file_id, os.O_RDWR|os.O_CREATE, 0644)
// Check for errors when opening the file
if err != nil {
log.Println("error on open or create file : ", err)
return ""
}
_, err = io.Copy(tmpFile, file)
if err != nil {
log.Println(err)
return ""
}
fmt.Println("tmpFile name : ", tmpFile.Name())
tmpFile.Close()
src, err := imaging.Open(thumbnail_path + "/" + file_id)
if err != nil {
log.Println("failed to open image: %v", err)
return ""
}
// Resize the cropped image to width = 200px preserving the aspect ratio.
src = imaging.Resize(src, 100, 0, imaging.Lanczos)
// Save the resulting image as PNG.
err = imaging.Save(src, thumbnail_path+"/"+file_id+".png")
if err != nil {
//log.Printlnf("failed to save image: %v", err)
fmt.Println("failed to save image: %v", err)
return ""
}
os.Remove(thumbnail_path + "/" + file_id)
thumb, err := os.Open(thumbnail_path + "/" + file_id + ".png")
if err != nil {
fmt.Println("failed to open thumbnail: %v", err)
return ""
}
defer thumb.Close()
fileInfo, _ := thumb.Stat()
var size int64 = fileInfo.Size()
bytes := make([]byte, size)
// read file into bytes
buffer := bufio.NewReader(thumb)
_, err = buffer.Read(bytes)
thumbnail := new(bson.Binary)
thumbnail.Kind = 0x00
thumbnail.Data = bytes
// Write thumbnail to mongo
colQuerier := bson.M{"gridfsid": file_id}
change := bson.M{"$set": bson.M{"thumbnail": thumbnail}}
err = mc.session.DB(mc.db).C("medias").Update(colQuerier, change)
if err != nil {
panic(err)
}
} else {
fmt.Println("media thumbnail exist in mongo")
tmpFile, err := os.OpenFile(thumbnail_path+"/"+file_id+".png", os.O_RDWR|os.O_CREATE, 0644)
// Check for errors when opening the file
if err != nil {
log.Println("error on open or create thumbnail : ", err)
return ""
}
defer tmpFile.Close()
_, err = io.Copy(tmpFile, bytes.NewReader(media.Thumbnail.Data))
if err != nil {
log.Println("error on write thumbnail to fs : ", err)
return ""
}
fmt.Println("tmpFile name : ", tmpFile.Name())
}
os.Remove(thumbnail + "/" + file_id)
}
return "/thumbnail/" + file_id + ".png"
}
func (mc MediaController) DownloadMedia(c echo.Context) error {
......@@ -278,11 +330,13 @@ func (mc MediaController) PlayFrontMediaChunk(c echo.Context) error {
//fmt.Println("n : ", n)
c.Response().Header().Set("Content-Disposition", "inline; filename=\""+file.Name()+"\"")
c.Response().Header().Set(echo.HeaderContentType, contentType.MIME.Value)
c.Response().Header().Set(echo.HeaderXContentTypeOptions, "nosniff")
c.Response().WriteHeader(http.StatusOK)
for err != io.EOF {
c.Response().Header().Set("Content-Length", strconv.Itoa(len(buffer)))
c.Response().Write(buffer)
c.Response().Flush()
n, err = file.Read(buffer)
......@@ -344,6 +398,12 @@ func (mc MediaController) PlayFrontMedia(c echo.Context) error {
fmt.Printf("File type: %s. MIME: %s\n", contentType.Extension, contentType.MIME.Value)
file.Seek(0, 0)
c.Response().Header().Set("Content-Length", strconv.Itoa(int(file.Size())))
c.Response().Header().Set("Content-Disposition", "inline; filename=\""+file.Name()+"\"")
c.Response().Header().Set(echo.HeaderContentType, contentType.MIME.Value)
c.Response().Header().Set(echo.HeaderXContentTypeOptions, "nosniff")
c.Response().WriteHeader(http.StatusOK)
return c.Stream(http.StatusOK, contentType.MIME.Value, file)
}
......
......@@ -13,6 +13,7 @@ type (
Tag string `json:"tag" bson:"tag"`
Description string `json:"description" bson:"description"`
Submedia []*Submedia `json:"submedia"`
Thumbnail bson.Binary `json:"thumbnail" bson:"thumbnail"`
}
Submedia struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment