-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachment.go
55 lines (49 loc) · 1.16 KB
/
attachment.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
package server
import (
"io"
"os"
"path"
"time"
"github.com/ecnepsnai/otto"
)
// Attachment describes a file for a script
type Attachment struct {
ID string `ds:"primary"`
Path string
Name string
MimeType string
Owner RunAs
Created time.Time
Modified time.Time
Mode uint32
Size uint64
}
// OttoFile return an otto common file
func (attachment Attachment) OttoFile() (*otto.File, error) {
f, err := os.OpenFile(attachment.FilePath(), os.O_RDONLY, 0644)
if err != nil {
log.Error("Error opening script file '%s': %s", attachment.ID, err.Error())
return nil, err
}
defer f.Close()
fileData, err := io.ReadAll(f)
if err != nil {
log.Error("Error reading file '%s': %s", attachment.ID, err.Error())
return nil, err
}
ottoFile := otto.File{
Path: attachment.Path,
Mode: attachment.Mode,
Owner: otto.RunAs{
UID: attachment.Owner.UID,
GID: attachment.Owner.GID,
Inherit: attachment.Owner.Inherit,
},
Data: fileData,
}
return &ottoFile, nil
}
// FilePath returns the absolute path for this attachment
func (attachment Attachment) FilePath() string {
return path.Join(Directories.Attachments, attachment.ID)
}