本文整理汇总了Golang中mime/multipart.Part类的典型用法代码示例。如果您正苦于以下问题:Golang Part类的具体用法?Golang Part怎么用?Golang Part使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Part类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: valCheck
/**
Check a value against a bounded(!) buffer
*/
func valCheck(buffer []byte, refVal []byte, checkedVal *multipart.Part) bool {
totalBytesRead := 0
bufferLength := len(buffer)
for {
if totalBytesRead >= bufferLength {
break
}
bytesRead, err := checkedVal.Read(buffer[totalBytesRead:])
if bytesRead < 0 || err == io.EOF {
break
}
totalBytesRead += bytesRead
}
i := 0
refValLength := len(refVal)
if totalBytesRead != refValLength {
return false
}
for i < refValLength {
if refVal[i] != buffer[i] {
return false
}
i++
}
return true
}
示例2: 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
}
示例3: 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
}
示例4: 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
}
示例5: 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
}
示例6: testMultipartInlineWithStdLib
func testMultipartInlineWithStdLib(t *testing.T, originalPart *Message, stdlibAltPart *multipart.Part) {
// confirm stdlib headers match our headers
if !reflect.DeepEqual(map[string][]string(originalPart.Header), map[string][]string(stdlibAltPart.Header)) {
t.Fatal("Message does not match its parsed counterpart")
}
// multipart/alternative with inlines should have text/plain and multipart/related parts
alternativeReader := multipart.NewReader(stdlibAltPart, boundary(map[string][]string(stdlibAltPart.Header)))
plainPart, err := alternativeReader.NextPart()
if err != nil {
t.Fatal("Couldn't get next part", err)
}
testBodyPartWithStdLib(t, originalPart.Parts[0], plainPart)
relatedPart, err := alternativeReader.NextPart()
if err != nil {
t.Fatal("Couldn't get next part", err)
}
testMultipartRelatedWithStdLib(t, originalPart.Parts[1], relatedPart)
// confirm EOF and Close
if _, err = alternativeReader.NextPart(); err != io.EOF || stdlibAltPart.Close() != nil {
t.Fatal("Should be EOF", err)
}
}
示例7: testMultipartRelatedWithStdLib
func testMultipartRelatedWithStdLib(t *testing.T, originalPart *Message, stdlibRelatedPart *multipart.Part) {
// confirm stdlib headers match our headers
if !reflect.DeepEqual(map[string][]string(originalPart.Header), map[string][]string(stdlibRelatedPart.Header)) {
t.Fatal("Message does not match its parsed counterpart")
}
// multipart/related should have text/html, image/gif, and image/png parts
relatedReader := multipart.NewReader(stdlibRelatedPart, boundary(map[string][]string(stdlibRelatedPart.Header)))
htmlPart, err := relatedReader.NextPart()
if err != nil {
t.Fatal("Couldn't get next part", err)
}
testBodyPartWithStdLib(t, originalPart.Parts[0], htmlPart)
gifPart, err := relatedReader.NextPart()
if err != nil {
t.Fatal("Couldn't get next part", err)
}
testBodyPartWithStdLib(t, originalPart.Parts[1], gifPart)
pngPart, err := relatedReader.NextPart()
if err != nil {
t.Fatal("Couldn't get next part", err)
}
testBodyPartWithStdLib(t, originalPart.Parts[2], pngPart)
// confirm EOF and Close
if _, err = relatedReader.NextPart(); err != io.EOF || stdlibRelatedPart.Close() != nil {
t.Fatal("Should be EOF", err)
}
}
示例8: upload
func upload(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
uploadTemplate.Execute(w, nil)
} else {
part_reader, err := r.MultipartReader()
if err != nil {
log.Println("get file:", err)
w.WriteHeader(http.StatusInternalServerError)
}
log.Println("start copy")
var file_part *multipart.Part
for {
if file_part, err = part_reader.NextPart(); err != nil {
if err == io.EOF {
err = nil
}
break
}
if file_part.FormName() == "file" {
if err = write_file(file_part); err != nil {
break
}
}
file_part.Close()
}
if err != nil {
log.Println("write file:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/upload", 302)
}
}
示例9: testBodyPartWithStdLib
func testBodyPartWithStdLib(t *testing.T, originalPart *Message, stdlibPart *multipart.Part) {
// decode base64 if exists
var stdlibPartBodyReader io.Reader
if stdlibPart.Header.Get("Content-Transfer-Encoding") == "base64" {
stdlibPart.Header.Del("Content-Transfer-Encoding")
stdlibPartBodyReader = base64.NewDecoder(base64.StdEncoding, stdlibPart)
} else {
stdlibPartBodyReader = stdlibPart
}
// confirm stdlib headers match our headers
if !reflect.DeepEqual(map[string][]string(originalPart.Header), map[string][]string(stdlibPart.Header)) {
t.Fatal("Message header does not match its parsed counterpart")
}
// read content
content, err := ioutil.ReadAll(stdlibPartBodyReader)
if err != nil || stdlibPart.Close() != nil {
t.Fatal("Couldn't read or close part body", err)
}
// confirm content is deeply equal
if !reflect.DeepEqual(originalPart.Body, content) {
t.Fatal("Message body does not match its parsed counterpart")
}
}
示例10: multipartUploadHandler
func multipartUploadHandler(r *http.Request) (albumName, contentType, fileName, contentLength string, fileReader io.Reader, err error) {
mbound, err := checkMultipart(r)
if err != nil {
return
}
// Count reader, counts bytes read as they are read.
creader := iomod.NewReadCounter(r.Body)
mreader := multipart.NewReader(creader, mbound)
sconlen := r.Header.Get("Content-Length")
conlen, err := strconv.Atoi(sconlen)
// Picasa REQUIRES Content-Length!
if err != nil {
log.Println("No Content-Length header or invalid value!", sconlen)
return
}
for {
var mpart *multipart.Part
mpart, err = mreader.NextPart()
if mpart != nil {
log.Println("Multipart handler:", mpart, mpart.FormName(), err)
} else {
return
}
conlen -= 1
name := mpart.FormName()
switch name {
case "album":
var albumNameBytes []byte
albumNameBytes, err = ioutil.ReadAll(mpart)
if err != nil {
log.Println("Error reading album name!", albumName, err)
return
}
log.Println("Read", creader.Count, "bytes so far ( content-length is", r.Header["Content-Length"], ")")
albumName = string(albumNameBytes)
case "Filedata":
contentType = mpart.Header.Get("Content-Type")
var mtypes map[string]string
_, mtypes, err = mime.ParseMediaType(mpart.Header.Get("Content-Disposition"))
if err != nil {
return
}
fileName = mtypes["filename"]
log.Println("Read", creader.Count, "bytes so far ( content-length is", r.Header.Get("Content-Length"), ")")
// We have to do this, because it seems like the only reliable way to determine the size of the file... Hopefully the files they send are not too large...
// WARNING: Security vunerability with large files, could overrun the server.
buf := new(bytes.Buffer)
io.Copy(buf, mpart)
fileReader = buf
contentLength = strconv.Itoa(buf.Len())
}
}
return
}
示例11: WriteFileChunk
func WriteFileChunk(chunk *multipart.Part, file *os.File) error {
buffer := make([]byte, 4096)
bufbytes, err := chunk.Read(buffer)
if err == io.EOF {
return err
}
file.Write(buffer[:bufbytes])
return err
}
示例12: 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[:],
}
}
示例13: 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
}
示例14: 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)
}
示例15: 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
}
}