本文整理汇总了Golang中mime/multipart.Part.FileName方法的典型用法代码示例。如果您正苦于以下问题:Golang Part.FileName方法的具体用法?Golang Part.FileName怎么用?Golang Part.FileName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mime/multipart.Part
的用法示例。
在下文中一共展示了Part.FileName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: extractHTTPFormValues
func extractHTTPFormValues(reader *multipart.Reader) (io.Reader, map[string]string, *probe.Error) {
/// HTML Form values
formValues := make(map[string]string)
filePart := new(bytes.Buffer)
var err error
for err == nil {
var part *multipart.Part
part, err = reader.NextPart()
if part != nil {
if part.FileName() == "" {
buffer, err := ioutil.ReadAll(part)
if err != nil {
return nil, nil, probe.NewError(err)
}
formValues[http.CanonicalHeaderKey(part.FormName())] = string(buffer)
} else {
_, err := io.Copy(filePart, part)
if err != nil {
return nil, nil, probe.NewError(err)
}
}
}
}
return filePart, formValues, nil
}
示例2: handleUpload
func handleUpload(r *http.Request, p *multipart.Part) (fi *FileInfo) {
fi = &FileInfo{
Name: p.FileName(),
Type: p.Header.Get("Content-Type"),
}
if !fi.ValidateType() {
return
}
defer func() {
if rec := recover(); rec != nil {
log.Println(rec)
fi.Error = rec.(error).Error()
}
}()
lr := &io.LimitedReader{R: p, N: MAX_FILE_SIZE + 1}
context := appengine.NewContext(r)
w, err := blobstore.Create(context, fi.Type)
defer func() {
w.Close()
fi.Size = MAX_FILE_SIZE + 1 - lr.N
fi.Key, err = w.Key()
check(err)
if !fi.ValidateSize() {
err := blobstore.Delete(context, fi.Key)
check(err)
return
}
delayedDelete(context, fi)
fi.CreateUrls(r, context)
}()
check(err)
_, err = io.Copy(w, lr)
return
}
示例3: extractPostPolicyFormValues
// Extract form fields and file data from a HTTP POST Policy
func extractPostPolicyFormValues(reader *multipart.Reader) (filePart io.Reader, fileName string, formValues map[string]string, err error) {
/// HTML Form values
formValues = make(map[string]string)
fileName = ""
for err == nil {
var part *multipart.Part
part, err = reader.NextPart()
if part != nil {
canonicalFormName := http.CanonicalHeaderKey(part.FormName())
if canonicalFormName != "File" {
var buffer []byte
limitReader := io.LimitReader(part, maxFormFieldSize+1)
buffer, err = ioutil.ReadAll(limitReader)
if err != nil {
return nil, "", nil, err
}
if int64(len(buffer)) > maxFormFieldSize {
return nil, "", nil, errSizeUnexpected
}
formValues[canonicalFormName] = string(buffer)
} else {
filePart = part
fileName = part.FileName()
// As described in S3 spec, we expect file to be the last form field
break
}
}
}
return filePart, fileName, formValues, nil
}
示例4: sendFileBlobs
func sendFileBlobs(c *blobserv.Client, part *multipart.Part) (respMeta map[string]interface{}) {
meta := blob.NewMeta()
defer func() {
respMeta = map[string]interface{}{}
respMeta["name"] = meta.Name
respMeta["size"] = meta.Size
if r := recover(); r != nil {
respMeta["error"] = r.(error).Error()
}
}()
obj := blob.NewObject()
meta.RcasObjectRef = obj.Ref()
meta.Name = part.FileName()
data, err := ioutil.ReadAll(part)
util.Check(err)
meta.Size = int64(len(data))
blobs := blob.SplitRaw(data, blob.DefaultChunkSize)
meta.ContentRefs = blob.RefsFor(blobs)
m, err := blob.Marshal(meta)
util.Check(err)
blobs = append(blobs, m, obj)
for _, b := range blobs {
err = c.PutBlob(b)
util.Check(err)
}
return respMeta
}
示例5: handleSaveSingleImage
// save a single image
func handleSaveSingleImage(part *multipart.Part) (info models.ImageInfo, err error) {
newID := bson.NewObjectId()
date := time.Now().Format("20060102")
err = helper.CreateDirIfNotExists(filepath.Join(helper.Config.SaveDir, date))
if err != nil {
return
}
path := filepath.Join(date, newID.Hex())
savePath := filepath.Join(helper.Config.SaveDir, path)
dst, err := os.Create(savePath)
if err != nil {
return
}
defer dst.Close()
var bytes int64
if bytes, err = io.Copy(dst, part); err != nil {
return
}
width, height := helper.GetImageDimensions(savePath)
var hash models.HashInfo
hash, err = helper.CalculateBasicHashes(savePath)
if err != nil {
return
}
URL := helper.Config.BaseURL + "/img/" + newID.Hex()
info = models.ImageInfo{
ID: newID,
Name: part.FileName(),
Extension: filepath.Ext(part.FileName()),
Path: path,
Width: width,
Height: height,
URL: URL,
Resizes: map[string]string{},
Hash: hash,
Size: bytes,
CreatedAt: time.Now(),
}
err = db.StoreImage(&info)
if err != nil {
return
}
return info, nil
}
示例6: readAttachmentFromMimePart
func readAttachmentFromMimePart(part *multipart.Part) NNTPAttachment {
hdr := part.Header
content_type := hdr.Get("Content-Type")
media_type, _, err := mime.ParseMediaType(content_type)
buff := new(bytes.Buffer)
fname := part.FileName()
idx := strings.LastIndex(fname, ".")
ext := ".txt"
if idx > 0 {
ext = fname[idx:]
}
transfer_encoding := hdr.Get("Content-Transfer-Encoding")
if transfer_encoding == "base64" {
// read the attachment entirely
io.Copy(buff, part)
// clear reference
part = nil
// allocate a buffer for the decoded part
att_bytes := make([]byte, base64.StdEncoding.DecodedLen(buff.Len()))
decoded_bytes := make([]byte, len(att_bytes))
// decode
_, err = base64.StdEncoding.Decode(decoded_bytes, buff.Bytes())
// reset original attachment buffer
buff.Reset()
// copy and wrap
copy(att_bytes, decoded_bytes)
buff = bytes.NewBuffer(att_bytes)
att_bytes = nil
// clear reference
decoded_bytes = nil
} else {
_, err = io.Copy(buff, part)
// clear reference
part = nil
}
if err != nil {
log.Println("failed to read attachment from mimepart", err)
return nil
}
sha := sha512.Sum512(buff.Bytes())
hashstr := base32.StdEncoding.EncodeToString(sha[:])
fpath := hashstr + ext
return nntpAttachment{
body: *buff,
header: hdr,
mime: media_type,
filename: fname,
filepath: fpath,
ext: ext,
hash: sha[:],
}
}
示例7: uploadPhoto
func uploadPhoto(part *multipart.Part, c *http.Cookie) *http.Cookie {
fmt.Printf("%v", part.FileName())
fName := getSha(part) + filepath.Ext(part.FileName())
wd, _ := os.Getwd()
path := filepath.Join(wd, "assets", "imgs", fName)
dst, _ := os.Create(path)
defer dst.Close()
//src.Seek(0,0)
io.Copy(dst, part)
return addPhoto(fName, c)
}
示例8: unpackPart
func unpackPart(part *multipart.Part, emlbase string, errors chan error) {
defer part.Close()
partFileName := part.FileName()
if partFileName == "" {
return
}
attachmentFileName := emlbase + " " + partFileName
attachmentFile, err := os.Create(attachmentFileName)
if err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem opening the %q file: %s",
attachmentFileName,
err.Error()))
return
}
defer attachmentFile.Close()
enc := part.Header.Get("Content-Transfer-Encoding")
var partReader io.Reader
switch enc {
case "", "7bit", "8bit":
partReader = part
case "base64", "BASE64", "Base64":
partReader = base64.NewDecoder(base64.StdEncoding, part)
default:
errors <- MessageError(
fmt.Sprintf(
"Attachment %q: unknown encoging %q",
attachmentFileName,
enc))
return
}
_, err = io.Copy(attachmentFile, partReader)
if err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem copying the %q part of the %q message: %s",
attachmentFile,
emlbase,
err.Error()))
return
}
}
示例9: checkPartFileExists
func checkPartFileExists(t *testing.T, part *multipart.Part) (ok, skipped bool) {
if part.FormName() != "file" {
return false, true
}
if part.FileName() != "file1.ext" {
t.Errorf("Filename not set")
return
}
return true, false
}
示例10: mimeTypeForPart
func mimeTypeForPart(part *multipart.Part) string {
if contentType := part.Header.Get("Content-Type"); contentType != "" {
if _, _, err := mime.ParseMediaType(contentType); err == nil {
return contentType
}
}
ext := path.Ext(part.FileName())
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "image/jpeg"
}
return mimeType
}
示例11: SetMultipart
func (d *tenpuInput) SetMultipart(part *multipart.Part) (isFile bool) {
if part.FileName() != "" {
d.FileName = part.FileName()
d.ContentType = part.Header["Content-Type"][0]
isFile = true
return
}
switch part.FormName() {
case "OwnerId":
d.OwnerId = formValue(part)
}
return
}
示例12: SetMultipart
func (this *ImageInput) SetMultipart(part *multipart.Part) (isFile bool) {
if part.FileName() != "" {
this.FileName = part.FileName()
this.ContentType = part.Header["Content-Type"][0]
isFile = true
return
}
switch part.FormName() {
case "OwnerId":
this.OwnerId = formValue(part)
}
return
}
示例13: handleUpload
func handleUpload(p *multipart.Part, dir string) (fi *fileInfo) {
fi = &fileInfo{
Name: p.FileName(),
Type: p.Header.Get("Content-Type"),
}
path := filepath.Clean(dir + "/" + fi.Name)
f, _ := os.Create(path)
io.Copy(f, p)
f.Close()
return
}
示例14: delayedDelete
/*
func delayedDelete(c appengine.Context, fi *FileInfo) {
if key := string(fi.Key); key != "" {
task := &taskqueue.Task{
Path: "/" + escape(key) + "/-",
Method: "DELETE",
Delay: time.Duration(EXPIRATION_TIME) * time.Second,
}
taskqueue.Add(c, task, "")
}
}
*/
func handleUpload(r *http.Request, p *multipart.Part) (fi *FileInfo) {
fi = &FileInfo{
Name: p.FileName(),
Type: p.Header.Get("Content-Type"),
}
if !fi.ValidateType() {
return
}
defer func() {
if rec := recover(); rec != nil {
fmt.Println(rec)
fi.Error = rec.(error).Error()
}
}()
lr := &io.LimitedReader{R: p, N: MAX_FILE_SIZE + 1}
err := os.MkdirAll(path.Join(UPLOAD_DIR,target_dir), 0777)
if err != nil {
fmt.Println(err)
}
f, err := os.Create(path.Join(UPLOAD_DIR,target_dir,p.FileName()))
/*
context := appengine.NewContext(r)
w, err := blobstore.Create(context, fi.Type)
*/
defer func() {
f.Close()
fi.Size = MAX_FILE_SIZE + 1 - lr.N
//fi.Key, err = w.Key()
//check(err)
/*
if !fi.ValidateSize() {
err := blobstore.Delete(context, fi.Key)
check(err)
return
}
delayedDelete(context, fi)
fi.CreateUrls(r, context)
*/
}()
check(err)
_, err = io.Copy(f, lr)
return
}
示例15: handleUpload
func handleUpload(r *http.Request, p *multipart.Part) (fi *FileInfo) {
fi = &FileInfo{
Name: p.FileName(),
Type: p.Header.Get("Content-Type"),
}
if !fi.ValidateType() {
return
}
defer func() {
if rec := recover(); rec != nil {
log.Println(rec)
fi.Error = rec.(os.Error).String()
}
}()
var b bytes.Buffer
lr := &io.LimitedReader{p, MAX_FILE_SIZE + 1}
context := appengine.NewContext(r)
w, err := blobstore.Create(context, fi.Type)
defer func() {
w.Close()
fi.Size = MAX_FILE_SIZE + 1 - lr.N
fi.Key, err = w.Key()
check(err)
if !fi.ValidateSize() {
err := blobstore.Delete(context, fi.Key)
check(err)
return
}
delayedDelete(context, fi)
if b.Len() > 0 {
fi.CreateThumbnail(&b, context)
}
fi.CreateUrls(r, context)
}()
check(err)
var wr io.Writer = w
if imageTypes.MatchString(fi.Type) {
wr = io.MultiWriter(&b, w)
}
_, err = io.Copy(wr, lr)
return
}