2015-09-27 22:17:12 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-07 22:45:34 -04:00
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/bzip2"
|
|
|
|
"compress/gzip"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2015-09-27 22:17:12 -04:00
|
|
|
"errors"
|
2015-10-07 22:45:34 -04:00
|
|
|
"io"
|
|
|
|
"sort"
|
2015-10-06 02:50:20 -04:00
|
|
|
"time"
|
2015-10-30 23:13:43 -04:00
|
|
|
"unicode"
|
2015-10-07 22:45:34 -04:00
|
|
|
|
2017-05-02 00:25:56 -04:00
|
|
|
"github.com/andreimarcu/linx-server/backends"
|
|
|
|
"github.com/andreimarcu/linx-server/expiry"
|
2015-10-07 22:45:34 -04:00
|
|
|
"github.com/dchest/uniuri"
|
2017-03-25 04:05:23 -04:00
|
|
|
"gopkg.in/h2non/filetype.v1"
|
2015-09-27 22:17:12 -04:00
|
|
|
)
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
var NotFoundErr = errors.New("File not found.")
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2017-05-02 00:25:56 -04:00
|
|
|
func generateMetadata(fName string, exp time.Time, delKey string) (m backends.Metadata, err error) {
|
2016-06-07 02:37:42 -04:00
|
|
|
file, err := fileBackend.Open(fName)
|
2015-09-27 22:17:12 -04:00
|
|
|
if err != nil {
|
2015-10-07 22:45:34 -04:00
|
|
|
return
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2016-06-07 02:37:42 -04:00
|
|
|
m.Size, err = fileBackend.Size(fName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
m.Expiry = exp
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
if delKey == "" {
|
|
|
|
m.DeleteKey = uniuri.NewLen(30)
|
|
|
|
} else {
|
|
|
|
m.DeleteKey = delKey
|
|
|
|
}
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
// Get first 512 bytes for mimetype detection
|
|
|
|
header := make([]byte, 512)
|
|
|
|
file.Read(header)
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2017-03-25 04:05:23 -04:00
|
|
|
kind, err := filetype.Match(header)
|
|
|
|
if err != nil {
|
|
|
|
m.Mimetype = "application/octet-stream"
|
|
|
|
} else {
|
|
|
|
m.Mimetype = kind.MIME.Value
|
|
|
|
}
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2015-10-28 15:21:54 -04:00
|
|
|
if m.Mimetype == "" {
|
|
|
|
// Check if the file seems anything like text
|
2015-10-30 23:13:43 -04:00
|
|
|
if printable(header) {
|
2015-10-28 15:21:54 -04:00
|
|
|
m.Mimetype = "text/plain"
|
|
|
|
} else {
|
|
|
|
m.Mimetype = "application/octet-stream"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
// Compute the sha256sum
|
|
|
|
hasher := sha256.New()
|
|
|
|
file.Seek(0, 0)
|
|
|
|
_, err = io.Copy(hasher, file)
|
|
|
|
if err == nil {
|
|
|
|
m.Sha256sum = hex.EncodeToString(hasher.Sum(nil))
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
2015-10-07 22:45:34 -04:00
|
|
|
file.Seek(0, 0)
|
|
|
|
|
|
|
|
// If archive, grab list of filenames
|
|
|
|
if m.Mimetype == "application/x-tar" {
|
|
|
|
tReadr := tar.NewReader(file)
|
|
|
|
for {
|
|
|
|
hdr, err := tReadr.Next()
|
|
|
|
if err == io.EOF || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeDir || hdr.Typeflag == tar.TypeReg {
|
|
|
|
m.ArchiveFiles = append(m.ArchiveFiles, hdr.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(m.ArchiveFiles)
|
|
|
|
} else if m.Mimetype == "application/x-gzip" {
|
|
|
|
gzf, err := gzip.NewReader(file)
|
|
|
|
if err == nil {
|
|
|
|
tReadr := tar.NewReader(gzf)
|
|
|
|
for {
|
|
|
|
hdr, err := tReadr.Next()
|
|
|
|
if err == io.EOF || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeDir || hdr.Typeflag == tar.TypeReg {
|
|
|
|
m.ArchiveFiles = append(m.ArchiveFiles, hdr.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(m.ArchiveFiles)
|
|
|
|
}
|
|
|
|
} else if m.Mimetype == "application/x-bzip" {
|
|
|
|
bzf := bzip2.NewReader(file)
|
|
|
|
tReadr := tar.NewReader(bzf)
|
|
|
|
for {
|
|
|
|
hdr, err := tReadr.Next()
|
|
|
|
if err == io.EOF || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeDir || hdr.Typeflag == tar.TypeReg {
|
|
|
|
m.ArchiveFiles = append(m.ArchiveFiles, hdr.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(m.ArchiveFiles)
|
|
|
|
} else if m.Mimetype == "application/zip" {
|
|
|
|
zf, err := zip.NewReader(file, m.Size)
|
|
|
|
if err == nil {
|
|
|
|
for _, f := range zf.File {
|
|
|
|
m.ArchiveFiles = append(m.ArchiveFiles, f.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(m.ArchiveFiles)
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
return
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:25:56 -04:00
|
|
|
func metadataWrite(filename string, metadata *backends.Metadata) error {
|
|
|
|
return metaBackend.Put(filename, metadata)
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:25:56 -04:00
|
|
|
func metadataRead(filename string) (metadata backends.Metadata, err error) {
|
|
|
|
metadata, err = metaBackend.Get(filename)
|
2015-10-07 22:45:34 -04:00
|
|
|
if err != nil {
|
|
|
|
// Metadata does not exist, generate one
|
2017-05-02 00:25:56 -04:00
|
|
|
newMData, err := generateMetadata(filename, expiry.NeverExpire, "")
|
2015-10-07 22:45:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return metadata, err
|
|
|
|
}
|
|
|
|
metadataWrite(filename, &newMData)
|
2016-06-07 02:37:42 -04:00
|
|
|
|
2017-05-02 00:25:56 -04:00
|
|
|
metadata, err = metaBackend.Get(filename)
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
2015-10-07 22:45:34 -04:00
|
|
|
|
|
|
|
return
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
2015-10-30 23:13:43 -04:00
|
|
|
|
|
|
|
func printable(data []byte) bool {
|
|
|
|
for i, b := range data {
|
|
|
|
r := rune(b)
|
|
|
|
|
|
|
|
// A null terminator that's not at the beginning of the file
|
|
|
|
if r == 0 && i == 0 {
|
|
|
|
return false
|
|
|
|
} else if r == 0 && i < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r > unicode.MaxASCII {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|