fix uploading and img rendering

This commit is contained in:
cyber-dream 2023-05-02 03:12:43 +03:00
parent a0ccc5e120
commit 8e2d014317
8 changed files with 151 additions and 167 deletions

View File

@ -1,105 +0,0 @@
package fileuploading
//sudo apt-get install libvips-tools
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"strings"
"github.com/disintegration/imaging"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type FileUploading struct {
fs *webfilesystem.WebFileSystem
}
func NewFileUploading(webfs *webfilesystem.WebFileSystem) *FileUploading {
return &FileUploading{
fs: webfs,
}
}
func (f *FileUploading) CreateMiniatures(parentDir string, file string) error {
src, err := imaging.Open(path.Join(parentDir, file))
if err != nil {
// log.Fatalf("failed to open image: %v", err)
return err
}
// Resize the cropped image to width = 200px preserving the aspect ratio.
src = imaging.Resize(src, 32, 0, imaging.Lanczos)
// Save the resulting image as JPEG.
splittedFileName := strings.Split(file, ".")
err = imaging.Save(src, path.Join(parentDir, splittedFileName[0]+"_32px."+splittedFileName[1]))
if err != nil {
// log.Fatalf("failed to save image: %v", err)
return err
}
data, err := os.ReadFile(path.Join(parentDir, file))
if err != nil {
return err
}
min32Data, err := os.ReadFile(path.Join(parentDir, splittedFileName[0]+"_32px."+splittedFileName[1]))
if err != nil {
return err
}
imgData := wde.Img{
Header: "",
Data: data,
Miniature32: min32Data,
}
newFile := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(),
Name: file,
Type: "picture",
Data: imgData,
Icon: "",
}
f.fs.CreateFile(&newFile, "/home/user/")
return nil
}
func (f *FileUploading) EncodeToBase64(path string) (string, string) {
// Read the entire file into a byte slice
bytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
var base64Encoding string
// Determine the content type of the image file
mimeType := http.DetectContentType(bytes)
// Prepend the appropriate URI scheme header depending
// on the MIME type
switch mimeType {
case "image/jpeg":
base64Encoding += "image/jpeg;base64"
case "image/png":
base64Encoding += "image/png;base64"
}
// Append the base64 encoded output
// base64Encoding += toBase64(bytes)
// Print the full base64 representation of the image
fmt.Println(base64Encoding)
return base64Encoding, toBase64(bytes)
}
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}

View File

@ -1,4 +1,4 @@
package wde
package libs
import (
"errors"
@ -12,18 +12,6 @@ type ImagLib struct {
}
func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
// header, ok := img.Data.(primitive.D).Map()["header"].(string)
// if !ok {
// return nil, errors.New("error in file decoding")
// }
// base64, ok := img.Data.(primitive.D).Map()["base64"].(string)
// if !ok {
// return nil, errors.New("error in file decoding")
// }
// base64min32, ok := img.Data.(primitive.D).Map()["base64min32"].(string)
// if !ok {
// return nil, errors.New("error in file decoding")
// }
data, ok := img.Data.(primitive.D).Map()["srcdata"]
if !ok {
return nil, errors.New("error in file decoding")

73
main.go
View File

@ -7,7 +7,7 @@ import (
"log"
"net/http"
"os"
"personalwebsite/fileuploading"
"personalwebsite/routewde"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
@ -25,7 +25,6 @@ import (
)
func main() {
if err := godotenv.Load(); err != nil {
println("No .env file found")
}
@ -67,7 +66,6 @@ func main() {
webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection)
webde := wde.NewWDE(webfs)
fileUploading := fileuploading.NewFileUploading(webfs)
persPropsApp := personalprops.NewPersPropsApp(webfs)
// finderApp := finder.FinderApplication{}
@ -82,58 +80,55 @@ func main() {
appsStorage.Apps["img-viewer"] = &imgViewerApp
appsStorage.Apps["blog-viewer"] = blogViewerApp
router.POST("/upload", func(c *gin.Context) {
router.POST("/upload", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// single file
file, _ := c.FormFile("file")
file, _ := ctx.FormFile("file")
// generateMins := c.Param("generateMins")
// log.Println(file.Filename)
// Upload the file to specific dst.
dst := "./test-img/" + file.Filename
c.SaveUploadedFile(file, dst)
ctx.SaveUploadedFile(file, dst)
fileUploading.CreateMiniatures("./test-img/", file.Filename)
err := webfs.UploadFile(dst, path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
}
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
// webfs.CreateFile(&img, "/home/user/")
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
router.GET("/testimg", func(c *gin.Context) {
// c.Data(200, "image/jpeg", data)
ctx.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
system := router.Group("system")
{
imgLibGroup := system.Group("imglib")
libsGroup := system.Group("libs")
{
imgLibGroup.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
min := ctx.Query("min")
imgLibGroup := libsGroup.Group("img")
{
imgLibGroup.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := webfs.Read(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
file, err := webfs.Read(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
}
img, err := wde.ReadImage(file)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
if min == "min32" {
ctx.Data(http.StatusOK, "image/jpeg", []byte(img.Miniature32))
} else {
ctx.Data(http.StatusOK, "image/jpeg", []byte(img.Data))
}
})
ctx.Data(200, "image/jpeg", file.Data.(primitive.Binary).Data)
})
}
}
wdeGroup := system.Group("wde")
{

View File

@ -1,6 +1,7 @@
package wde
import (
"path"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
@ -31,19 +32,23 @@ func (w *WDE) Render(path string) (gin.H, error) {
}, nil
}
func (w *WDE) RenderFileTileView(path string) (gin.H, error) {
list, err := w.fs.List(path)
func (w *WDE) RenderFileTileView(directory string) (gin.H, error) {
list, err := w.fs.List(directory)
if err != nil {
return nil, err
}
for _, file := range list {
switch file.Type {
case "directory":
file.Icon = "TODO"
case "base64img":
file.Icon = "http://localhost:8080/system/imglib/get?path=/home/user/cat2-test.jpeg&min=min32"
file.Icon = "http://localhost:8080/system/libs/img/get?path=/wde/icons/macos9/folder.png" //FIXME
case "jpeg":
fallthrough
case "png":
fallthrough
case "jpg":
file.Icon = "http://localhost:8080/system/libs/img/get?path=" + path.Join(directory, file.Name) //FIXME
default:
file.Icon = "http://localhost:8080/system/imglib/get?path=/home/user/cat2-test.jpeg&min=min32"
file.Icon = "http://localhost:8080/system/libs/img/get?path=/wde/icons/macos9/about-me.png" //FIXME
}
}
return gin.H{

View File

@ -0,0 +1,94 @@
package webfilesystem
//sudo apt-get install libvips-tools
import (
"errors"
"os"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
file, err := os.ReadFile(realFilepath)
if err != nil {
return err
}
splittedPath := fs.SplitPath(realFilepath)
fileName := splittedPath[len(splittedPath)-1]
extension := fs.GetExtension(fileName)
switch extension {
case "jpg":
fallthrough
case "jpeg":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.CreateFile(&newFile, path)
return err
case "png":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "png",
Data: file,
}
err := fs.CreateFile(&newFile, path)
return err
default:
return errors.New("this filetype not allowed")
}
}
// func (fs *WebFileSystem) CreateMiniatures(parentDir string, file string) error {
// src, err := imaging.Open(path.Join(parentDir, file))
// if err != nil {
// // log.Fatalf("failed to open image: %v", err)
// return err
// }
// // Resize the cropped image to width = 200px preserving the aspect ratio.
// src = imaging.Resize(src, 32, 0, imaging.Lanczos)
// // Save the resulting image as JPEG.
// splittedFileName := strings.Split(file, ".")
// err = imaging.Save(src, path.Join(parentDir, splittedFileName[0]+"_32px."+splittedFileName[1]))
// if err != nil {
// // log.Fatalf("failed to save image: %v", err)
// return err
// }
// data, err := os.ReadFile(path.Join(parentDir, file))
// if err != nil {
// return err
// }
// min32Data, err := os.ReadFile(path.Join(parentDir, splittedFileName[0]+"_32px."+splittedFileName[1]))
// if err != nil {
// return err
// }
// imgData := wde.Img{
// Header: "",
// Data: data,
// Miniature32: min32Data,
// }
// newFile := WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: file,
// Type: "picture",
// Data: imgData,
// Icon: "",
// }
// fs.CreateFile(&newFile, "/home/user/")
// return nil
// }
// func toBase64(b []byte) string {
// return base64.StdEncoding.EncodeToString(b)
// }

View File

@ -159,6 +159,13 @@ func (fs *WebFileSystem) SplitPath(path string) []string {
return resPath
}
func (fs *WebFileSystem) GetExtension(filename string) string {
// extension := []string{}
splittedName := strings.Split(filename, ".")
return splittedName[len(splittedName)-1]
}
func (fs *WebFileSystem) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")

View File

@ -1,7 +1,7 @@
package imgviewer
import (
"personalwebsite/wde"
"personalwebsite/libs"
"personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
@ -36,7 +36,7 @@ func (p *ImgViewerApp) Render(path string, isMobile bool) (gin.H, error) {
if err != nil {
return nil, err
}
data, err := wde.ReadImage(img)
data, err := libs.ReadImage(img)
if err != nil {
return nil, err
}

View File

@ -2,7 +2,7 @@ package personalprops
import (
"errors"
"personalwebsite/wde"
"personalwebsite/libs"
"personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
@ -194,7 +194,7 @@ func (p *PersonalPropertiesApp) Render() (gin.H, error) {
type HeaderIsland struct {
Name string
Icon *wde.Base64Img
Icon *libs.Base64Img
Info1 string
Info2 string
}