-
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.
Merge pull request #75 from krishicks/add-unpack-ability
Add unpack ability
- Loading branch information
Showing
5 changed files
with
460 additions
and
75 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.