当前位置: 首页>>代码示例>>Golang>>正文


Golang FileHeader.Open方法代码示例

本文整理汇总了Golang中mime/multipart.FileHeader.Open方法的典型用法代码示例。如果您正苦于以下问题:Golang FileHeader.Open方法的具体用法?Golang FileHeader.Open怎么用?Golang FileHeader.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mime/multipart.FileHeader的用法示例。


在下文中一共展示了FileHeader.Open方法的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
}
开发者ID:joshuarubin,项目名称:goscribe,代码行数:32,代码来源:web.go

示例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)

}
开发者ID:intfrr,项目名称:sendto,代码行数:27,代码来源:files.go

示例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
}
开发者ID:kiloe,项目名称:blobstore,代码行数:32,代码来源:server.go

示例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
}
开发者ID:tomyhero,项目名称:billie,代码行数:25,代码来源:slack.go

示例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
}
开发者ID:bbh-labs,项目名称:openinnovation-old,代码行数:25,代码来源:httputil.go

示例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
}
开发者ID:hypertornado,项目名称:prago,代码行数:25,代码来源:files.go

示例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

}
开发者ID:kolawoletech,项目名称:kaku-go,代码行数:60,代码来源:utils.go

示例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
}
开发者ID:x4rMa,项目名称:admin-1,代码行数:25,代码来源:file.go

示例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
}
开发者ID:dominique-locatelli,项目名称:bind,代码行数:7,代码来源:binder_test.go

示例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
}
开发者ID:hawx,项目名称:alexandria,代码行数:59,代码来源:upload.go

示例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)
}
开发者ID:dullgiulio,项目名称:dododada,代码行数:8,代码来源:http.go

示例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)
}
开发者ID:davidkbainbridge,项目名称:test2,代码行数:9,代码来源:testservice.go

示例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)
}
开发者ID:lambdaX,项目名称:rsc.godev,代码行数:12,代码来源:rietveld_test.go

示例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
}
开发者ID:ZJvandeWeg,项目名称:platform,代码行数:51,代码来源:emoji.go

示例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
	})
}
开发者ID:jessevdk,项目名称:autobuild,代码行数:14,代码来源:webqueue.go


注:本文中的mime/multipart.FileHeader.Open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。