github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/helper/filesystem/file.go (about) 1 package filesystem 2 3 import ( 4 "io" 5 "mime/multipart" 6 "os" 7 ) 8 9 // File represents a file received from client. 10 type File struct { 11 Header *multipart.FileHeader 12 MIMEType string 13 Data multipart.File 14 } 15 16 // Save writes the given file on the disk. 17 // Appends a timestamp to the given file name to avoid duplicate file names. 18 // The file is not readable anymore once saved as its FileReader has already been 19 // closed. 20 // 21 // Returns the actual file name. 22 func (file *File) Save(path string, name string) string { 23 name = timestampFileName(name) 24 writer, err := os.OpenFile(path+string(os.PathSeparator)+name, os.O_WRONLY|os.O_CREATE, 0660) 25 if err != nil { 26 panic(err) 27 } 28 defer writer.Close() 29 _, errCopy := io.Copy(writer, file.Data) 30 if errCopy != nil { 31 panic(errCopy) 32 } 33 file.Data.Close() 34 return name 35 }