func s3Upload(basePath string, data *multipart.FileHeader) (string, error) { if s3Bucket == nil { return "", fmt.Errorf("upload unavailable") } fileType := data.Header.Get("Content-Type") file, err := data.Open() defer file.Close() if err != nil { return "", err } fileData, err := ioutil.ReadAll(file) if err != nil { return "", err } h := sha1.New() if _, err := h.Write(fileData); err != nil { return "", err } filePath := fmt.Sprintf("/%s/%x", basePath, h.Sum(nil)) if err := s3Bucket.Put(filePath, fileData, fileType, s3.PublicRead); err != nil { return "", err } return s3BaseURL + filePath, nil }
// Copy form file to Blob func upload(f *multipart.FileHeader) (b *blob.Blob, err error) { // Open upload, err := f.Open() if err != nil { return } defer upload.Close() // Create blob b = blob.New() // Set filename from request b.Name = f.Filename // Set content-type from request if ct := f.Header.Get("Content-Type"); ct != "" { b.ContentType = ct } // Guess content-type from extension if missing if b.ContentType == "" || b.ContentType == ApplicationOctetStream { if ext := filepath.Ext(b.Name); ext != "" { b.ContentType = mime.TypeByExtension(ext) } } if b.ContentType == "" { b.ContentType = ApplicationOctetStream } // Write err = b.WriteFrom(upload) if err != nil { return } return }
func headerData(h *multipart.FileHeader) string { file, err := h.Open() if err != nil { return "ERROR: " + err.Error() } data, err := ioutil.ReadAll(file) file.Close() if err != nil { return "ERROR: " + err.Error() } return string(data) }
func loadFile(folder, path string, header *multipart.FileHeader) error { if !strings.HasPrefix(path, folder) { return errors.New("folder path should be prefix of path") } err := os.MkdirAll(folder, 0777) if err != nil { return err } inFile, err := header.Open() if err != nil { return err } outFile, err := os.Create(path) if err != nil { return err } io.Copy(outFile, inFile) defer outFile.Close() defer inFile.Close() return nil }
func openFileHeader(t *testing.T, fh *multipart.FileHeader) multipart.File { var r, err = fh.Open() if err != nil { t.Error(err) } return r }
func (f *FileField) HandleFile(file *multipart.FileHeader) (string, error) { reader, err := file.Open() if err != nil { return "", err } filename := file.Filename if len(f.UploadTo) > 0 { _, err := os.Stat(f.UploadTo) if err != nil { os.MkdirAll(f.UploadTo, 0777) } filename = fmt.Sprintf("%v/%v", f.UploadTo, file.Filename) } dst, err := os.Create(filename) if err != nil { return "", err } io.Copy(dst, reader) dst.Close() reader.Close() return filename, nil }
// SaveFile saves files to disk func (m *File) saveFile(fh *multipart.FileHeader) error { // Retreive the form image data by opening the referenced tmp file. f, err := fh.Open() if err != nil { return err } // If we have no path, set it to a default value /files/documents/id/name if len(m.Path) == 0 { err = m.NewFilePath(fh) if err != nil { return err } } // Make sure our path exists first err = file.CreatePathTo(m.Path) if err != nil { return err } // Write out our file to disk return file.Save(f, m.Path) }
func (c *appContext) uploadPic(a *multipart.FileHeader) (string, string) { log.Println("In upload pic territory") bucket := c.bucket file, err := a.Open() defer file.Close() if err != nil { panic(err.Error()) } if err != nil { panic(err) } buf, _ := ioutil.ReadAll(file) fn := uuid.New() fname := "places/" + fn + path.Ext(a.Filename) thumbname := "placesthumb/" + fn + path.Ext(a.Filename) log.Println(fname) b := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + fname d := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + thumbname filetype := http.DetectContentType(buf) err = bucket.Put(fname, buf, filetype, s3.PublicRead) if err != nil { log.Println("bucket put error for main image") panic(err.Error()) } log.Print("added a full image") img, err := jpeg.Decode(bytes.NewReader(buf)) if err != nil { log.Println(err.Error()) } m := resize.Resize(200, 200, img, resize.Lanczos2) buf2 := new(bytes.Buffer) err = jpeg.Encode(buf2, m, nil) if err != nil { fmt.Println(err.Error()) } thumb := buf2.Bytes() filetype2 := http.DetectContentType(thumb) err = bucket.Put(thumbname, thumb, filetype2, s3.PublicRead) if err != nil { log.Println("bucket put error for thumbnail") panic(err.Error()) } log.Println("uploaded one thumb image") return b, d }
func saveFile(header *multipart.FileHeader, destination string) error { file, err := header.Open() if err != nil { return err } defer file.Close() dir := path.Dir(destination) if err = os.MkdirAll(dir, os.ModeDir|0700); err != nil { return err } var output *os.File if output, err = os.OpenFile(destination, os.O_CREATE|os.O_WRONLY, 0600); err != nil { fmt.Println("test:", err) return err } var n int64 if n, err = io.Copy(output, file); err != nil { return err } log.Println("written", n, "bytes of data to file:", destination) return nil }
func upSlack(api *slack.Client, attachment *multipart.FileHeader) (string, error) { upFile, err := ioutil.TempFile("", "upSlack_") defer os.Remove(upFile.Name()) f, err := attachment.Open() if err != nil { return "", fmt.Errorf("attached file open error: %v", err) } // file save written, err := io.Copy(upFile, f) if err != nil { return "", fmt.Errorf("file save error: %v, written: %d", err, written) } fileInfo, err := api.UploadFile(slack.FileUploadParameters{ File: upFile.Name(), Filename: attachment.Filename, }) if err != nil { return "", fmt.Errorf("file upload error: %v", err) } return fileInfo.URL, nil }
func (h uploadHandler) doUpload(fileheader *multipart.FileHeader) error { file, err := fileheader.Open() defer file.Close() if err != nil { return err } contentType := fileheader.Header["Content-Type"][0] edition := &models.Edition{ Id: uuid.New(), ContentType: contentType, } dstPath := path.Join(h.bookPath, edition.Path()) dst, err := os.Create(dstPath) if err != nil { return err } defer dst.Close() if _, err := io.Copy(dst, file); err != nil { return err } opened, err := fileheader.Open() if err != nil { return err } if contentType != models.MOBI && contentType != models.EPUB { return errors.New("Format not supported: " + contentType) } metaFunc := metadata.Epub if contentType == models.MOBI { metaFunc = metadata.Mobi } meta, _ := metaFunc(opened) newBook := models.Book{ Id: uuid.New(), Added: time.Now(), Title: meta.Title, Author: meta.Author, Editions: models.Editions{edition}, } h.db.Save(newBook) h.es.Add(newBook) go h.convert(contentType, newBook) return nil }
func (s *server) postFile(f *multipart.FileHeader) error { fh, err := f.Open() if err != nil { return err } defer fh.Close() return s.cons.readAll(fh) }
func readMultipart(upload *multipart.FileHeader) ([]byte, error) { file, err := upload.Open() if err != nil { return nil, err } defer file.Close() reader := bufio.NewReader(file) return ioutil.ReadAll(reader) }
func uploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError { file, err := imageData.Open() if err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "") } defer file.Close() buf := bytes.NewBuffer(nil) io.Copy(buf, file) // make sure the file is an image and is within the required dimensions if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, err.Error()) } else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight { data := buf.Bytes() newbuf := bytes.NewBuffer(nil) if info, err := model.GetInfoForBytes(imageData.Filename, data); err != nil { return err } else if info.MimeType == "image/gif" { if gif_data, err := gif.DecodeAll(bytes.NewReader(data)); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_decode_error", nil, "") } else { resized_gif := resizeEmojiGif(gif_data) if err := gif.EncodeAll(newbuf, resized_gif); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "") } if err := app.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil { return err } } } else { if img, _, err := image.Decode(bytes.NewReader(data)); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.decode_error", nil, "") } else { resized_image := resizeEmoji(img, config.Width, config.Height) if err := png.Encode(newbuf, resized_image); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "") } if err := app.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil { return err } } } } else { if err := app.WriteFile(buf.Bytes(), getEmojiImagePath(id)); err != nil { return err } } return nil }
func WebQueueStage(file *multipart.FileHeader, uid uint32) (*PackageInfo, error) { return builder.Stage(file.Filename, uid, func(b *PackageBuilder, writer io.Writer) error { f, err := file.Open() if err != nil { return err } defer f.Close() _, err = io.Copy(writer, f) return err }) }
func copy(fh *multipart.FileHeader, path, fname string) error { buf := new(bytes.Buffer) file, err := fh.Open() if err != nil { return err } _, err = buf.ReadFrom(file) if err != nil { return err } err = os.MkdirAll(path, 0644) if err != nil { return err } return ioutil.WriteFile(filepath.Join(path, fname), buf.Bytes(), os.FileMode(0644)) }
func (fe *MfsFileExplorer) Upload(path string, file *multipart.FileHeader) error { fi, err := file.Open() defer fi.Close() if err != nil { return err } fileNew := NormalizePath(path + "/" + file.Filename) fo, err := os.OpenFile(fileNew, os.O_WRONLY|os.O_CREATE, 0777) defer fo.Close() if err != nil { return err } io.Copy(fo, fi) return nil }
func fileInfo(header *multipart.FileHeader) FileInfo { info := FileInfo{ Name: header.Filename, Type: header.Header.Get("Content-Type"), } if f, err := header.Open(); err == nil { info.Size, err = f.Seek(0, os.SEEK_END) if err != nil { log.Println("Parse file info, seek error", err) } f.Close() } else { log.Println("Parse file info, open error", err) } return info }
func saveUploadForQueue(header *multipart.FileHeader, qid string) error { file, err := header.Open() defer file.Close() if err != nil { return err } os.MkdirAll("tmp/"+qid, 0777) dst, err := os.Create("tmp/" + qid + "/" + header.Filename) defer dst.Close() if err != nil { return err } if _, err := io.Copy(dst, file); err != nil { return err } return nil }
func saveFile(fil *multipart.FileHeader, userPath string) { file, err := fil.Open() if err != nil { log.Println(err) return } defer file.Close() dest, err := os.Create(userPath + "/" + fil.Filename) if err != nil { log.Println(err) return } defer dest.Close() if _, err := io.Copy(dest, file); err != nil { log.Println(err) return } }
func unpackFileHeaderData(fh *multipart.FileHeader, t *testing.T) (data string) { if fh == nil { return } f, err := fh.Open() if err != nil { t.Error(err) } defer f.Close() var fb bytes.Buffer _, err = fb.ReadFrom(f) if err != nil { t.Error(err) } return fb.String() }
func fileHeaderContent(fh *multipart.FileHeader) string { if fh == nil { return "" } f, err := fh.Open() defer f.Close() if err != nil { panic("error opening FileHeader. Error:" + err.Error()) } var b bytes.Buffer _, err = b.ReadFrom(f) if err != nil { panic("error reading from FileHeader. Error:" + err.Error()) } return b.String() }
func unpackFileHeaderData(fh *multipart.FileHeader) string { if fh == nil { return "" } f, err := fh.Open() if err != nil { panic("Could not open file header:" + err.Error()) } defer f.Close() var fb bytes.Buffer _, err = fb.ReadFrom(f) if err != nil { panic("Could not read from file header:" + err.Error()) } return fb.String() }
// SaveMultipartFile saves multipart file fh under the given filename path. func SaveMultipartFile(fh *multipart.FileHeader, path string) error { f, err := fh.Open() if err != nil { return err } defer f.Close() if ff, ok := f.(*os.File); ok { return os.Rename(ff.Name(), path) } ff, err := os.Create(path) if err != nil { return err } defer ff.Close() _, err = copyZeroAlloc(ff, f) return err }
func UploadFile(realFile *multipart.FileHeader) (file *File, err error) { var source multipart.File if source, err = realFile.Open(); err != nil { return } defer source.Close() var img image.Config if img, _, err = image.DecodeConfig(source); err != nil { return } source.Seek(0, 0) hasher := sha1.New() io.Copy(hasher, source) fileKey := hex.EncodeToString(hasher.Sum(nil)) file = &File{Key: fileKey} has, _ := engine.Get(file) if !has { var dst *os.File var fileType = realFile.Header.Get("content-type") var ext, ok = config.FILE_EXTS[fileType] if !ok { ext = ".jpg" } if dst, err = os.Create(config.UPLOADPATH + fileKey + ext); err != nil { return } defer dst.Close() source.Seek(0, 0) if _, err = io.Copy(dst, source); err != nil { return } file.Width = img.Width file.Height = img.Height file.Type = fileType if _, err = engine.Insert(file); err != nil { return } } return }
func saveMediaFile(header *multipart.FileHeader, name string) error { ext := path.Ext(header.Filename) mimeType := mime.TypeByExtension(ext) isImage := strings.HasPrefix(mimeType, "image") isVideo := strings.HasPrefix(mimeType, "video") if (isImage || isVideo) == false { return nil } file, err := header.Open() if err != nil { return err } defer file.Close() // Create the 'content' directory if doesn't exist if err = prepareContentDirectory(); err != nil { return err } var filepath string if name != "" { filepath = "content/" + name + ext } else { filepath = "content/" + header.Filename } output, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { os.Remove(filepath) return err } defer output.Close() if _, err = io.Copy(output, file); err != nil { os.Remove(filepath) return err } return nil }
func (uf *UploadFile) uploadFileToLocal(fileHeader *multipart.FileHeader) (err error) { fullpath, name := uf.getPath(fileHeader.Filename) file, err := fileHeader.Open() if err != nil { return err } defer file.Close() if err := uf.saveToLocal(file, fullpath); err != nil { return err } uf.files = append(uf.files, models.File{ Name: fileHeader.Filename, Url: uf.cdn + "/" + path.Join(uf.url, name), Type: uint(uf.fileType), RelId: uf.relId, }) return nil }
func ExtractZip(f *multipart.FileHeader, d string) error { file, err := f.Open() defer file.Close() if err != nil { return err } size, err := file.Seek(0, 2) if err != nil { return err } r, err := zip.NewReader(file, size) if err != nil { return err } for _, f := range r.File { err := CopyZipFile(f, d, f.Name) if err != nil { return err } } return nil }
// Create inserts a new image record in the database and returns the id func Create(params map[string]string, fh *multipart.FileHeader) (int64, error) { // Remove params not in AllowedParams params = model.CleanParams(params, AllowedParams()) err := validateParams(params) if err != nil { return 0, err } // Update/add some params by default params["created_at"] = query.TimeString(time.Now().UTC()) params["updated_at"] = query.TimeString(time.Now().UTC()) id, err := Query().Insert(params) if fh != nil && id != 0 { // Retreive the form image data by opening the referenced tmp file f, err := fh.Open() if err != nil { return id, err } // Now retrieve the image concerned, and save the file representations image, err := Find(id) if err != nil { return id, err } // Save files to disk using the passed in file data (if any) err = image.SaveImageRepresentations(f) if err != nil { return id, err } } return id, err }
func uploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError { file, err := imageData.Open() if err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "") } defer file.Close() buf := bytes.NewBuffer(nil) io.Copy(buf, file) // make sure the file is an image and is within the required dimensions if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, err.Error()) } else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight { return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.app_error", nil, "") } if err := WriteFile(buf.Bytes(), getEmojiImagePath(id)); err != nil { return err } return nil }