fsys/file.go

56 lines
1.1 KiB
Go
Raw Normal View History

2024-11-17 16:00:48 +01:00
package fsys
import (
"bytes"
"github.com/minio/minio-go/v7"
"io"
"mime"
"os"
"path/filepath"
)
type File struct {
storage *Storage
file io.ReadSeekCloser
Name string
}
func (f *File) Read(p []byte) (n int, err error) {
return f.file.Read(p)
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
return f.file.Seek(offset, whence)
}
func (f *File) Write(p []byte) (n int, err error) {
if osFile, ok := f.file.(*os.File); ok {
return osFile.Write(p)
} else {
var fileContent []byte
if _, err := f.Read(fileContent); err != nil && err != io.EOF {
return 0, err
}
fileContent = append(fileContent, p...)
mimeType := mime.TypeByExtension(filepath.Ext(f.Name))
if mimeType == "" {
mimeType = "application/octet-stream"
}
2024-11-18 22:34:07 +01:00
_, err = f.storage.s3Client.PutObject(f.storage.ctx, f.storage.config.S3BucketName, f.Name, bytes.NewBuffer(fileContent), int64(len(fileContent)), minio.PutObjectOptions{
2024-11-17 16:00:48 +01:00
ContentType: mimeType,
})
if err != nil {
return 0, err
}
return len(p), nil
}
}
func (f *File) Close() error {
return f.file.Close()
}