-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuploadedfile.go
105 lines (91 loc) · 2.43 KB
/
uploadedfile.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
package frodo
import (
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
"reflect"
)
// FileUploadsPath for now, declares the path to upload the files
var FileUploadsPath = "./assets/uploads/"
// UploadedFile struct/type is the data that makes up an uploaded file
// once it is recieved and parsed eg. using request.FormFile()
type UploadedFile struct {
multipart.File
*multipart.FileHeader
/*
type FileHeader struct {
Filename string
Header textproto.MIMEHeader
}
*/
}
// Name returns the name of the file when it was uploaded
func (file *UploadedFile) Name() string {
// found in *multipart.FileHeader
return file.Filename
}
// Size returns the size of the file in question
func (file *UploadedFile) Size() int64 {
defer file.Close()
return file.Size()
}
// Extension returns the extension of the file uploaded
func (file *UploadedFile) Extension() string {
// _, header, error := r.FormFile(name)
ext := filepath.Ext(file.Filename)
return ext
}
// Move basically moves/transfers the uploaded file to the upload folder provided
//
// Using ...interface{} because I want the user to only pass more than one argument
// when changing upload dir and filename, if none is changed then defaults are used
//
// eg. file.Move(true)
// ----- or -----
// file.Move("../new_upload_path/", "newfilename.png")
//
func (file *UploadedFile) Move(args ...interface{}) bool {
file.Open()
defer file.Close()
name := args[0]
val := reflect.ValueOf(name)
// If a string was give, then treat is a the FileUploadsPath
if val.Kind().String() == "string" {
FileUploadsPath = name.(string)
}
var FileName string
// Check to see if a file name was given, 2nd argument
if len(args) > 1 {
FileName = args[1].(string)
} else {
FileName = file.Name()
}
savedFile, err := os.OpenFile(FileUploadsPath+FileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return false
}
_, ioerr := io.Copy(savedFile, file)
if ioerr != nil {
fmt.Println(ioerr)
return false
}
return true
}
// MimeType returns the mime/type of the file uploaded
func (file *UploadedFile) MimeType() string {
mimetype := file.Header.Get("Content-Type")
return mimetype
}
// IsValid checks if the file is alright by opening it up
// if errors come up while opening it is an invalid upload
func (file *UploadedFile) IsValid() bool {
_, err := file.Open()
defer file.Close()
if err != nil {
return false
}
return true
}