-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
190 lines (159 loc) · 5.12 KB
/
file.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package fstream
import (
"fmt"
"image"
"image/jpeg"
"io"
"log"
"math"
"mime/multipart"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
)
// File struct is final face of uploaded file, it includes necessary field to use them after file is uploaded
type File struct {
// Original uploaded file name
FileName string
// FileUniqueName is unique name
FileUniqueName string
// Uploaded file path
FilePath string
// Uploaded file extension
FileExtension string
// Uploaded file size
FileSize string
}
// RFileRequest struct is used for http request, use this struct to bind uploaded file
type RFileRequest struct {
// File is an interface to access the file part of a multipart message.
File multipart.File
// A FileHeader describes a file part of a multipart request.
UploadFile *multipart.FileHeader
// Maximum range of chunk uploads
MaxRange int
// Uploaded file size
FileSize int
// Upload directory
UploadDirectory string
// FileUniqueName is identifier to generate unique name for files
FileUniqueName bool
}
// uniqueName function generates unique string using UUID
func uniqueName(fileName string) string {
ext := filepath.Ext(fileName)
id, err := uuid.NewUUID()
if err != nil {
log.Fatalln(err)
}
return fmt.Sprintf("%s%s", id.String(), ext)
}
// RemoveUploadedFile removes uploaded file from uploaded directory and returns error if something went wrong,
// it takes upload directory and fileName.
// Use this function in your handler after file is uploaded
func RemoveUploadedFile(uploadDir, fileName string) error {
filePath := filepath.Join(uploadDir, fileName)
err := os.Remove(filePath)
if err != nil {
return err
}
return nil
}
// prettyByteSize function is used to concrete the file size
func prettyByteSize(b int) string {
bf := float64(b)
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
if math.Abs(bf) < 1024.0 {
return fmt.Sprintf("%3.1f%sB", bf, unit)
}
bf /= 1024.0
}
return fmt.Sprintf("%.1fYiB", bf)
}
// StoreChunk cares slice of chunks and returns final results and error.
// Functions creates new directory for chunks if it doesn't exist,
// if directory already exists it appends received chunks in current chunks and if entire file is uploaded then File struct is returned
func StoreChunk(r *RFileRequest) (*File, error) {
var rFile *File
// Create new directory for uploaded chunk
filePath := filepath.Join(r.UploadDirectory + r.UploadFile.Filename)
// Create new directory if it doesn't exist
if _, err := os.Stat(r.UploadDirectory); os.IsNotExist(err) {
err = os.MkdirAll(r.UploadDirectory, 0777)
if err != nil {
return nil, fmt.Errorf("failed to create new temporary directory: %v", err)
}
}
// Open the file for appending and creating if it doesn't exist
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, fmt.Errorf("error opening file: %v", err)
}
defer func() {
if cerr := f.Close(); cerr != nil {
log.Printf("error closing file: %v", cerr)
}
}()
// Copy the chunk data to the file
if _, err = io.Copy(f, r.File); err != nil {
return nil, fmt.Errorf("failed to copying file: %v", err)
}
// If the entire file is uploaded, finalize entire process and return file information
if r.MaxRange >= r.FileSize {
fileInfo, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stating file: %v", err)
}
// Calculate file size in bytes
size := prettyByteSize(int(fileInfo.Size()))
// Bind File struct and return
rFile = &File{
FileName: r.UploadFile.Filename,
FilePath: filePath,
FileExtension: filepath.Ext(r.UploadFile.Filename),
FileSize: size,
}
// Check if FileUniqueName field is true to generate unique name for file
if r.FileUniqueName {
uName := uniqueName(r.UploadFile.Filename)
rFile.FileUniqueName = uName
}
}
return rFile, nil
}
// IsAllowExtension checks if a given file's extension is allowed based on a provided list of acceptable extensions.
func IsAllowExtension(fileExtensions []string, fileName string) bool {
ext := strings.ToLower(filepath.Ext(fileName))
// range over the received extensions to check if file is ok to accept
for _, allowed := range fileExtensions {
if ext == allowed {
return true
}
}
return false
}
// RemoveExifMetadata returns error if something went wrong during the exif metadata removal process, functions takes inputPath which is location of the image.
// purpose of this function is that to open and re-encode image without metadata
func RemoveExifMetadata(inputPath string) error {
// open input path file
file, err := os.Open(inputPath)
if err != nil {
return fmt.Errorf("failed to open image: %v", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return fmt.Errorf("failed to decode image: %v", err)
}
output, err := os.Create(inputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer output.Close()
// re-encode image without metadata
if err = jpeg.Encode(output, img, &jpeg.Options{Quality: 100}); err != nil {
return fmt.Errorf("failed to encode image: %v", err)
}
return nil
}