本文整理汇总了Golang中mime/multipart.File.Seek方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Seek方法的具体用法?Golang File.Seek怎么用?Golang File.Seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mime/multipart.File
的用法示例。
在下文中一共展示了File.Seek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: uploadFile
func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {
ext, err := fileFilter(req, hdr)
if err != nil {
return "", err
}
name := getSha(mpf) + `.` + ext
rand.Seed(time.Now().Unix())
x := rand.Intn(3)
fmt.Println(x)
var pfx string
switch x {
case 0:
pfx = "bob/"
case 1:
pfx = "james/"
case 2:
pfx = "stacey/"
}
name = pfx + name
mpf.Seek(0, 0)
ctx := appengine.NewContext(req)
return name, putFile(ctx, name, mpf)
}
示例2: generateThumbnails
func generateThumbnails(file multipart.File, userId, pathPrefix string, bucket *s3.Bucket) error {
file.Seek(0, 0)
img, _, err := image.Decode(file)
if err != nil {
log.Println("Error decoding image", err)
return err
}
var buf bytes.Buffer
for _, i := range THUMBNAIL_SIZES {
resized := resize.Resize(uint(i), 0, img, resize.Lanczos3)
err = jpeg.Encode(&buf, resized, nil)
if err != nil {
return err
}
path := fmt.Sprintf("%s/%d.jpg", pathPrefix, i)
err = bucket.Put(path, buf.Bytes(), "image/jpeg", s3.PublicRead)
if err != nil {
return err
}
buf.Reset()
}
return err
}
示例3: ThumbnailMultipart
/**
takes a multipart file. Does not close it. creates a thumbnail and returns the file path
*/
func ThumbnailMultipart(file multipart.File, fileName string) (string, error) {
var (
err error
img image.Image
thumbImg *image.NRGBA
)
if _, err := file.Seek(0, 0); err != nil {
log.Printf("failed to seek to beginning of img " + err.Error())
return "", err
}
var thumbPath string = "/tmp/" + time.Now().String() + fileName
img, _, err = image.Decode(file)
if nil != err {
log.Printf("failed to decode img " + err.Error())
return "", err
}
thumbImg = imaging.Thumbnail(img, 300, 300, imaging.Lanczos)
out, err := os.Create(thumbPath)
if nil != err {
log.Printf("failed to create thumb path " + err.Error())
return "", err
}
defer out.Close()
// write new image to file
err = jpeg.Encode(out, thumbImg, nil)
return thumbPath, err
}
示例4: Create
// Create takes the Profile Folder, a content-type (passed to S3), and the binary file
// data, and saves the file to S3, then saving the S3 information to the database.
// It returns the number of bytes sent to S3, for no particular reason, and any error.
func (p *Photo) Create(folder, ctype string, file multipart.File) (int64, error) {
end, err := file.Seek(0, 2)
if err != nil {
return 0, err
}
_, err = file.Seek(0, 0)
if err != nil {
return 0, err
}
// ensure that folder actually exists in S3, here!
b := s3Photos.Bucket(BaseBucket + "/" + folder)
// ensure that folder exists... this doesn't error if it's already there
err = b.PutBucket(s3.Private)
if err != nil {
return 0, err
}
err = b.PutReader(p.Href, file, end, ctype, s3.Private)
if err != nil {
return 0, err
}
err = dbmap.Insert(p)
if err != nil {
return 0, err
}
return end, nil
}
示例5: uploadPhoto
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
defer src.Close()
fName := getSha(src) + ".jpg"
wd, _ := os.Getwd()
path := filepath.Join(wd, "assets", "imgs", fName)
dst, _ := os.Create(path)
defer dst.Close()
src.Seek(0, 0)
io.Copy(dst, src)
return addPhoto(fName, c)
}
示例6: uploadPhoto
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, session *sessions.Session) {
defer src.Close()
fName := getSha(src) + ".jpg"
wd, _ := os.Getwd()
path := filepath.Join(wd, "assets", "imgs", fName)
dst, _ := os.Create(path)
defer dst.Close()
src.Seek(0, 0)
io.Copy(dst, src)
addPhoto(fName, session)
}
示例7: uploadPhoto
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
defer src.Close()
fName := getSha(src) + filepath.Ext(hdr.Filename)
wd, _ := os.Getwd()
m := Model(c)
path := filepath.Join(wd, "assets", "imgs", m.Name, fName)
dst, _ := os.Create(path)
defer dst.Close()
src.Seek(0, 0)
io.Copy(dst, src)
return addPhoto("/imgs/"+m.Name+"/"+fName, c)
}
示例8: uploadPhoto
func uploadPhoto(m map[string]string, src multipart.File, hdr *multipart.FileHeader) map[string]string {
defer src.Close()
fName := getSha(src) + ".jpg"
wd, _ := os.Getwd()
path := filepath.Join(wd, "assets", "imgs", fName)
dst, _ := os.Create(path)
defer dst.Close()
src.Seek(0, 0)
io.Copy(dst, src)
addPhoto(m, fName)
return m
}
示例9: uploadFile
func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {
ext, err := fileFilter(req, hdr)
if err != nil {
return "", err
}
name := getSha(mpf) + `.` + ext
mpf.Seek(0, 0)
ctx := appengine.NewContext(req)
return name, putFile(ctx, name, mpf)
}
示例10: storeFile
func storeFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {
ext, err := fileType(req, hdr) //get type of file based on file extension
if err != nil {
return "", err
}
name := getSha(mpf) + `.` + ext //give unique name to the file
mpf.Seek(0, 0)
ctx := appengine.NewContext(req)
return name, store(ctx, name, mpf)
}
示例11: cropField
func cropField(field *gorm.Field, scope *gorm.Scope) (cropped bool) {
if field.Field.CanAddr() {
// TODO Handle scanner
if media, ok := field.Field.Addr().Interface().(Media); ok && !media.Cropped() {
option := parseTagOption(field.Tag.Get("media_library"))
if media.GetFileHeader() != nil || media.NeedCrop() {
var file multipart.File
var err error
if fileHeader := media.GetFileHeader(); fileHeader != nil {
file, err = media.GetFileHeader().Open()
} else {
file, err = media.Retrieve(media.URL("original"))
}
if err != nil {
scope.Err(err)
return false
}
media.Cropped(true)
if url := media.GetURL(option, scope, field, media); url == "" {
scope.Err(errors.New("invalid URL"))
} else {
result, _ := json.Marshal(map[string]string{"Url": url})
media.Scan(string(result))
}
if file != nil {
defer file.Close()
var handled = false
for _, handler := range mediaHandlers {
if handler.CouldHandle(media) {
file.Seek(0, 0)
if scope.Err(handler.Handle(media, file, option)) == nil {
handled = true
}
}
}
// Save File
if !handled {
scope.Err(media.Store(media.URL(), option, file))
}
}
return true
}
}
}
return false
}
示例12: decodePNG
func decodePNG(f multipart.File) (*Image, error) {
var image Image
ic, err := png.DecodeConfig(f)
_, err = f.Seek(0, 0)
i, err := png.Decode(f)
if err != nil {
return nil, fmt.Errorf("Could not decode PNG file.")
}
image.Config = ic
image.Image = i
return &image, nil
}
示例13: UploadFile
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
}
示例14: judgeContentType
func judgeContentType(f multipart.File) (string, string, error) {
f.Seek(0, 0)
buf := make([]byte, 16)
count, err := f.Read(buf)
if err != nil {
return "", "", err
}
if count >= 10 && string(buf[6:10]) == "JFIF" {
return "image/jpeg", ".jpg", nil
}
if count >= 4 && string(buf[0:3]) == "GIF" {
return "image/gif", ".gif", nil
}
if count >= 2 && string(buf[1:4]) == "PNG" {
return "image/png", ".png", nil
}
return "application/octet-stream", "", nil
}
示例15: isSupported
func isSupported(file multipart.File) (bool, string, error) {
buff := make([]byte, 512)
_, err := file.Read(buff)
// Reset the file
file.Seek(0, 0)
if err != nil {
return false, "", err
}
filetype := http.DetectContentType(buff)
for _, i := range supportedFileTypes {
if i == filetype {
return true, filetype, nil
}
}
log.Println("File not supported: " + filetype)
return false, filetype, nil
}