-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unpack option to InRequest params
If the InRequest is configured with `Params{Unpack: true}`, unpack the archive after downloading it. The original archive is not stored. In other words, if you have an archive, `some-archive.tgz`, with a file `some-file` in it, the destination directory after downloading and unpacking will contain just `some-file`.
- Loading branch information
Showing
5 changed files
with
424 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package in | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"bitbucket.org/taruti/mimemagic" | ||
) | ||
|
||
var archiveMimetypes = []string{ | ||
"application/x-gzip", | ||
"application/gzip", | ||
"application/x-tar", | ||
"application/zip", | ||
} | ||
|
||
func mimetype(r *bufio.Reader) (string, error) { | ||
bs, err := r.Peek(512) | ||
if err != nil && err != io.EOF { | ||
return "", err | ||
} | ||
|
||
if len(bs) == 0 { | ||
return "", errors.New("cannot determine mimetype from empty bytes") | ||
} | ||
|
||
return mimemagic.Match("", bs), nil | ||
} | ||
|
||
func archiveMimetype(filename string) string { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return "" | ||
} | ||
defer f.Close() | ||
|
||
mime, err := mimetype(bufio.NewReader(f)) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
for i := range archiveMimetypes { | ||
if strings.HasPrefix(mime, archiveMimetypes[i]) { | ||
return archiveMimetypes[i] | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func inflate(mime, path, destination string) error { | ||
var cmd *exec.Cmd | ||
|
||
switch mime { | ||
case "application/zip": | ||
cmd = exec.Command("unzip", "-P", "", "-d", destination, path) | ||
defer os.Remove(path) | ||
|
||
case "application/x-tar": | ||
cmd = exec.Command("tar", "xf", path, "-C", destination) | ||
defer os.Remove(path) | ||
|
||
case "application/gzip", "application/x-gzip": | ||
cmd = exec.Command("gunzip", path) | ||
|
||
default: | ||
return fmt.Errorf("don't know how to extract %s", mime) | ||
} | ||
|
||
return cmd.Run() | ||
} |
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
Oops, something went wrong.