-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.go
158 lines (134 loc) · 3.23 KB
/
convert.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package vfs
import (
"fmt"
"path"
"path/filepath"
"strings"
"github.com/vmkteam/vfs/db"
)
const AtomTime = "02.01.2006 15:04"
type Folder struct {
ID int `json:"id"`
Name string `json:"name"`
ParentID *int `json:"parentId"`
Folders []Folder `json:"folders,omitempty"`
}
type FileParams struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
type File struct {
ID int `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
PreviewPath string `json:"previewpath"`
RelPath string `json:"relpath"`
Size int `json:"size"`
SizeH []string `json:"sizeH"`
Date string `json:"date"`
Type string `json:"type"`
Extension string `json:"extension"`
Params FileParams `json:"params"`
Shortpath string `json:"shortpath"`
Width *int `json:"width"`
Height *int `json:"height"`
}
func NewFolder(in *db.VfsFolder) *Folder {
if in == nil {
return nil
}
return &Folder{
ID: in.ID,
Name: in.Title,
ParentID: in.ParentFolderID,
}
}
func NewFile(in *db.VfsFile, webpath, previewpath string) *File {
if in == nil {
return nil
}
fz := 0
var hz []string
if in.FileSize != nil {
fz = *in.FileSize
size, unit := getSizeAndUnit(float64(fz))
hz = []string{fmt.Sprintf("%.*g", 4, size), unit}
}
var width, height *int
fp := FileParams{}
if in.Params != nil {
fp.Width = in.Params.Width
fp.Height = in.Params.Height
if fp.Width != 0 {
width = &fp.Width
}
if fp.Height != 0 {
height = &fp.Height
}
}
extension := strings.TrimPrefix(filepath.Ext(in.Path), ".")
preview := ""
if isImage(extension) {
preview = path.Join(previewpath, in.Path)
}
return &File{
ID: in.ID,
Name: in.Title,
PreviewPath: preview,
Path: path.Join(webpath, in.Path),
RelPath: strings.TrimSuffix(filepath.Base(in.Path), filepath.Ext(in.Path)),
Size: fz,
SizeH: hz,
Date: in.CreatedAt.Format(AtomTime),
Type: in.MimeType,
Extension: extension,
Params: fp,
Shortpath: in.Path,
Width: width,
Height: height,
}
}
func NewFullFolder(in *db.VfsFolder, childFolders []db.VfsFolder) *Folder {
if in == nil {
return nil
}
f := NewFolder(in)
if len(childFolders) > 0 {
f.Folders = make([]Folder, len(childFolders))
for i, cf := range childFolders {
f.Folders[i] = *NewFolder(&cf)
}
}
return f
}
func getSizeAndUnit(size float64) (newSize float64, unit string) {
_map := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
base := 1000.0
i := 0
unitsLimit := len(_map) - 1
for size >= base && i < unitsLimit {
size /= base
i++
}
return size, _map[i]
}
func isImage(ext string) bool {
for _, imageExt := range []string{"jpg", "jpeg", "gif", "png", "webp", "bmp", "tiff"} {
if imageExt == ext {
return true
}
}
return false
}
type HelpUploadItem struct {
URL string `json:"url"`
Params []string `json:"params"`
}
type HelpUploadResponse struct {
Temp HelpUploadItem `json:"temp"`
Queue HelpUploadItem `json:"queue"`
}
type UrlByHashListResponse struct {
Hash string `json:"hash"`
WebPath string `json:"webPath"`
}