本文整理汇总了Golang中mime/multipart.FileHeader类的典型用法代码示例。如果您正苦于以下问题:Golang FileHeader类的具体用法?Golang FileHeader怎么用?Golang FileHeader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileHeader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: s3Upload
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
}
示例2: saveFile
// 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)
}
示例3: upload
// 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
}
示例4: upSlack
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
}
示例5: saveFile
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
}
示例6: loadFile
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
}
示例7: uploadPic
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
}
示例8: HandleFile
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
}
示例9: openFileHeader
func openFileHeader(t *testing.T, fh *multipart.FileHeader) multipart.File {
var r, err = fh.Open()
if err != nil {
t.Error(err)
}
return r
}
示例10: doUpload
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
}
示例11: postFile
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)
}
示例12: readMultipart
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)
}
示例13: headerData
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)
}
示例14: uploadEmojiImage
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
}
示例15: WebQueueStage
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
})
}