This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,428 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ host: localhost | |
# readonly-host: localhost | ||
bind: "0.0.0.0:8000" | ||
uid: git | ||
api: | ||
request: | ||
maxMemory: 2048 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2014 gandalf authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package multipartzip | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"io" | ||
"log" | ||
"mime/multipart" | ||
"path/filepath" | ||
) | ||
|
||
func CreateZipBuffer(files []struct{ Name, Body string }) (*bytes.Buffer, error) { | ||
buf := new(bytes.Buffer) | ||
w := zip.NewWriter(buf) | ||
for _, file := range files { | ||
f, err := w.Create(file.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = f.Write([]byte(file.Body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
err := w.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf, nil | ||
} | ||
|
||
func StreamWriteMultipartForm(params map[string]string, fileField, path, boundary string, pw *io.PipeWriter, buf *bytes.Buffer) { | ||
defer pw.Close() | ||
mpw := multipart.NewWriter(pw) | ||
mpw.SetBoundary(boundary) | ||
if fileField != "" && path != "" { | ||
fw, err := mpw.CreateFormFile(fileField, filepath.Base(path)) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
if buf != nil { | ||
_, err = io.Copy(fw, buf) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
} | ||
} | ||
for key, val := range params { | ||
_ = mpw.WriteField(key, val) | ||
} | ||
err := mpw.Close() | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2014 gandalf authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package multipartzip | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"github.com/tsuru/gandalf/fs" | ||
"io" | ||
"mime/multipart" | ||
"os" | ||
"path" | ||
) | ||
|
||
func ValueField(f *multipart.Form, n string) (string, error) { | ||
v, ok := f.Value[n] | ||
if !ok { | ||
return "", fmt.Errorf("Invalid value field %q", n) | ||
} | ||
if len(v) != 1 { | ||
return "", fmt.Errorf("Not a single value %q", n) | ||
} | ||
if len(v[0]) == 0 { | ||
return "", fmt.Errorf("Empty value %q", n) | ||
} | ||
return v[0], nil | ||
} | ||
|
||
func FileField(f *multipart.Form, n string) (*multipart.FileHeader, error) { | ||
v, ok := f.File[n] | ||
if !ok { | ||
return nil, fmt.Errorf("Invalid file field %q", n) | ||
} | ||
if len(v) != 1 { | ||
return nil, fmt.Errorf("Not a single file %q", n) | ||
} | ||
return v[0], nil | ||
} | ||
|
||
func CopyZipFile(f *zip.File, d, p string) error { | ||
if p != "" { | ||
dirname := path.Dir(p) | ||
if dirname != "." { | ||
err := os.MkdirAll(d+"/"+dirname, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
rc, err := f.Open() | ||
defer rc.Close() | ||
if err != nil { | ||
return err | ||
} | ||
path := d + "/" + p | ||
stat, err := os.Stat(path) | ||
if err != nil || !stat.IsDir() { | ||
file, err := fs.Filesystem().OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
_, err = io.Copy(file, rc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func ExtractZip(f *multipart.FileHeader, d string) error { | ||
file, err := f.Open() | ||
defer file.Close() | ||
if err != nil { | ||
return err | ||
} | ||
size, err := file.Seek(0, 2) | ||
if err != nil { | ||
return err | ||
} | ||
r, err := zip.NewReader(file, size) | ||
if err != nil { | ||
return err | ||
} | ||
for _, f := range r.File { | ||
err := CopyZipFile(f, d, f.Name) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.