Skip to content

Commit

Permalink
feat: support upload with file mode
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-jerry committed Aug 7, 2023
1 parent f39a657 commit 4564913
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
8 changes: 7 additions & 1 deletion config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ type AppConfig struct {
Port string
AdminName string
AdminPassword string
Minio Minio

// memory or file
UploadMode string

Minio Minio
}

var Conf AppConfig
Expand All @@ -22,12 +26,14 @@ func init() {
viper.SetDefault("app.port", "8080")
viper.SetDefault("app.admin.username", "admin")
viper.SetDefault("app.admin.password", "password")
viper.SetDefault("app.uploadMode", "memory")

viper.ReadInConfig()

Conf.Port = viper.GetString("app.port")
Conf.AdminName = viper.GetString("app.admin.username")
Conf.AdminPassword = viper.GetString("app.admin.password")
Conf.UploadMode = viper.GetString("app.uploadMode")

Conf.Minio = Minio{
Endpoint: viper.GetString("minio.endpoint"),
Expand Down
85 changes: 79 additions & 6 deletions minio/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package minio

import (
"context"
"crypto/md5"
"fmt"
"io"
"log"
"mindav/config"
"os"
"path"
"strings"
Expand All @@ -24,19 +26,16 @@ func (mo *file) Stat() (os.FileInfo, error) {
}

func (mo *file) ReadFrom(r io.Reader) (n int64, err error) {
ctx := context.Background()

log.Println("Start uploading:", mo.name)

info, err := mo.fs.client.PutObject(ctx, mo.fs.BucketName, strings.TrimPrefix(mo.name, "/"), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
n, err = mo.upload(r)

if err != nil {
log.Println("Upload failed", err)
return 0, err
log.Println("upload failed", err)
return 0, nil
}

n = info.Size

log.Println("Successfully uploaded bytes: ", n)

return n, nil
Expand Down Expand Up @@ -76,3 +75,77 @@ func (mo *file) Readdir(count int) (fileInfoList []os.FileInfo, err error) {

return fileInfoList, err
}

func (mo *file) upload(r io.Reader) (n int64, err error) {

if config.Conf.UploadMode == "memory" {
return mo.uploadInMemoryMode(r)
}

return mo.uploadInFileMode(r)
}

func (mo *file) uploadInMemoryMode(r io.Reader) (n int64, err error) {
ctx := context.Background()

info, err := mo.fs.client.PutObject(ctx, mo.fs.BucketName, strings.TrimPrefix(mo.name, "/"), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})

if err != nil {
return 0, err
}

return info.Size, nil

}

func (mo *file) uploadInFileMode(r io.Reader) (n int64, err error) {
ctx := context.Background()

md5 := fmt.Sprintf("%x", md5.Sum([]byte(mo.name)))

tmpFilePath := path.Join("./tmp", md5)

err = os.MkdirAll("./tmp", os.ModePerm)

if err != nil {
return 0, err
}

f, err := os.Create(tmpFilePath)
if err != nil {
return 0, err
}

defer f.Close()
defer func(p string) {
err = os.RemoveAll(p)
if err != nil {
log.Println("remove file failed", tmpFilePath)
}
}(tmpFilePath)

buf := make([]byte, 1024)
for {
// read a chunk
n, err := r.Read(buf)
if err != nil && err != io.EOF {
return 0, err
}
if n == 0 {
break
}

// write a chunk
if _, err := f.Write(buf[:n]); err != nil {
return 0, err
}
}

info, err := mo.fs.client.FPutObject(ctx, mo.fs.BucketName, strings.TrimPrefix(mo.name, "/"), tmpFilePath, minio.PutObjectOptions{ContentType: "application/octet-stream"})

if err != nil {
return 0, nil
}

return info.Size, nil
}

0 comments on commit 4564913

Please sign in to comment.