package webfilesystem //sudo apt-get install libvips-tools import ( "errors" "os" "go.mongodb.org/mongo-driver/bson/primitive" ) func (fs *WebFileSystem) UploadFile(realFilepath string, filePath string) error { //TODO check is file exists // parentPath := fs.GetParentPath(filePath) file, err := os.ReadFile(realFilepath) if err != nil { return err } splittedPath := fs.SplitPath(realFilepath) fileName := splittedPath[len(splittedPath)-1] extension := fs.GetExtension(fileName) newFile2 := FileHeader{ MongoId: primitive.NewObjectID(), Name: fileName, Type: "", Icon: "", Data: primitive.NilObjectID, } newFileData := BinaryFileData{ MongoId: primitive.NewObjectID(), Bin: file, } // filePath := path.Join(parentPath, fileName) switch extension { case "jpg": fallthrough case "jpeg": newFile2.Type = "jpeg" _, _, err := fs.Write(filePath, &newFile2, &newFileData) return err case "png": newFile2.Type = "png" _, _, err := fs.Write(filePath, &newFile2, &newFileData) if err != nil { println(err.Error()) return err } return nil 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) // }