fsys/file.go
2024-11-18 16:34:07 -05:00

55 lines
1.1 KiB
Go

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"
}
_, err = f.storage.s3Client.PutObject(f.storage.ctx, f.storage.config.S3BucketName, f.Name, bytes.NewBuffer(fileContent), int64(len(fileContent)), minio.PutObjectOptions{
ContentType: mimeType,
})
if err != nil {
return 0, err
}
return len(p), nil
}
}
func (f *File) Close() error {
return f.file.Close()
}