Skip to content

Commit

Permalink
Merge pull request #2 from hrntknr/add-inmemory-option
Browse files Browse the repository at this point in the history
add in memory option
  • Loading branch information
hrntknr authored Mar 23, 2023
2 parents 74b2528 + 2a2f608 commit 0d35b51
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
50 changes: 46 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"bytes"
"context"
"fmt"
"io/fs"
"log"
"os"
"strings"
"sync"
Expand All @@ -21,6 +23,7 @@ type File struct {
lock *sync.Mutex
cacheObject *minio.Object
writeBuffer *bytes.Buffer
writeTmp *os.File
flag int
}

Expand All @@ -34,11 +37,13 @@ func newFile(ctx context.Context, mc *minio.Client, path []string, dummyDirs []s
lock: &sync.Mutex{},
cacheObject: nil,
writeBuffer: bytes.NewBuffer([]byte{}),
writeTmp: nil,
flag: flag,
}
}

func (f *File) Readdir(count int) ([]fs.FileInfo, error) {
fmt.Println("Readdir", f.path, count)
if len(f.path) == 0 {
list, err := f.mc.ListBuckets(context.Background())
if err != nil {
Expand Down Expand Up @@ -163,13 +168,24 @@ func (f *File) Stat() (fs.FileInfo, error) {
isDir: true,
}, nil
} else {
if f.writeBuffer.Len() > 0 {
if inMemory && f.writeBuffer.Len() > 0 {
return &FileInfo{
name: "",
size: int64(f.writeBuffer.Len()),
modTime: time.Now(),
isDir: false,
}, nil
} else if !inMemory && f.writeTmp != nil {
stat, err := f.writeTmp.Stat()
if err != nil {
return nil, err
}
return &FileInfo{
name: "",
size: stat.Size(),
modTime: time.Now(),
isDir: false,
}, nil
} else {
return &FileInfo{
name: "",
Expand Down Expand Up @@ -223,10 +239,28 @@ func (f *File) Write(p []byte) (n int, err error) {
return 0, os.ErrInvalid
}
if f.offset != 0 {
f.writeBuffer.Reset()
if inMemory {
f.writeBuffer.Reset()
} else {
if f.writeTmp != nil {
f.writeTmp.Close()
f.writeTmp = nil
}
}
return 0, os.ErrInvalid
}
return f.writeBuffer.Write(p)
if inMemory {
return f.writeBuffer.Write(p)
} else {
if f.writeTmp == nil {
file, err := os.CreateTemp("", "s3dav-proxy")
if err != nil {
return 0, os.ErrInvalid
}
f.writeTmp = file
}
return f.writeTmp.Write(p)
}
}

func (f *File) Seek(offset int64, whence int) (int64, error) {
Expand All @@ -246,11 +280,19 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
}

func (f *File) Close() error {
if f.writeBuffer.Len() > 0 {
if inMemory && f.writeBuffer.Len() > 0 {
if _, err := f.mc.PutObject(f.ctx, f.path[0], strings.Join(f.path[1:], "/"), f.writeBuffer, int64(f.writeBuffer.Len()), minio.PutObjectOptions{}); err != nil {
return handleMinioError(err)
}
}
if !inMemory && f.writeTmp != nil {
if _, err := f.mc.PutObject(f.ctx, f.path[0], strings.Join(f.path[1:], "/"), f.writeTmp, 0, minio.PutObjectOptions{}); err != nil {
return handleMinioError(err)
}
if err := os.Remove(f.writeTmp.Name()); err != nil {
log.Println(err)
}
}
if f.cacheObject != nil {
if err := f.cacheObject.Close(); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
preferDirectory bool // Minio does not allow duplicate names for directory and file names, but s3 does.
allowBucketsOps bool
readOnly bool
inMemory bool
verbose bool
tlsCert string
tlsKey string
Expand Down Expand Up @@ -115,6 +116,7 @@ func init() {
RootCmd.Flags().BoolVarP(&preferDirectory, "prefer-directory", "d", true, "Prefer directory over file")
RootCmd.Flags().BoolVarP(&allowBucketsOps, "allow-buckets-ops", "b", false, "Allow operations on buckets")
RootCmd.Flags().BoolVarP(&readOnly, "ro", "", false, "Read only")
RootCmd.Flags().BoolVarP(&inMemory, "in-memory", "m", false, "In memory(upload)")
RootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
RootCmd.Flags().StringVarP(&tlsCert, "tls-cert", "c", "", "TLS certificate")
RootCmd.Flags().StringVarP(&tlsKey, "tls-key", "k", "", "TLS key")
Expand Down
2 changes: 2 additions & 0 deletions webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -56,6 +57,7 @@ func (d *Handler) Mkdir(ctx context.Context, name string, perm os.FileMode) erro
}

func (d *Handler) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
fmt.Println("OpenFile", name, flag, perm)
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
names := splitPath(name)
return newFile(ctx, mc, names, d.dummyDirs, flag), nil
Expand Down

0 comments on commit 0d35b51

Please sign in to comment.