-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_blob.go
61 lines (49 loc) · 1.33 KB
/
file_blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package mahi
import (
"context"
"io"
"os"
"strings"
)
type FileBlobStorage interface {
Upload(ctx context.Context, bucket string, b *FileBlob) error
CreateBucket(ctx context.Context, bucket string) error
FileBlob(ctx context.Context, bucket, id, tempDir string) (*FileBlob, error)
}
// FileBlob is holds the properties needed for the blob of a file.
type FileBlob struct {
ID string
Data io.ReadCloser
MIMEValue string
Size int64
// TempFileName this is used to determine if we need to delete the temp file after using the FileBlob
TempFileName string
}
// Bytes transforms the data of the FileBlob into a byte array
func (b *FileBlob) Bytes(p []byte) (int, error) {
n, err := b.Data.Read(p)
if err != nil && err != io.EOF {
return 0, err
}
if int64(n) != b.Size {
return 0, io.ErrShortWrite
}
return n, nil
}
// Close closes the io.ReadCloser and deletes the temp file if one was used
func (b *FileBlob) Close() error {
if err := b.Data.Close(); err != nil {
return err
}
if b.TempFileName != "" {
return os.RemoveAll(b.TempFileName)
}
return nil
}
func (b *FileBlob) IsImage() bool {
mimeType := strings.Split(b.MIMEValue, "/")[0]
return mimeType == "image" && b.MIMEValue != "image/svg+xml"
}
func (b *FileBlob) IsTransformable() bool {
return b.IsImage() || b.MIMEValue == "application/pdf"
}