short URLs
This commit is contained in:
parent
03fb2da41c
commit
9e6ffc5d82
38
Metadata.go
38
Metadata.go
@ -2,13 +2,21 @@ package contented
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
|
"github.com/speps/go-hashids"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
hashIdSalt = "contented"
|
||||||
|
hashIdMinLength = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
type Metadata struct {
|
type Metadata struct {
|
||||||
|
FileHash string
|
||||||
FileSize int64
|
FileSize int64
|
||||||
UploadTime time.Time
|
UploadTime time.Time
|
||||||
UploadIP string
|
UploadIP string
|
||||||
@ -33,13 +41,35 @@ func (this *Server) Metadata(fileID string) (*Metadata, error) {
|
|||||||
return &m, nil
|
return &m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Server) SetMetadata(fileID string, m Metadata) error {
|
func idToString(v uint64) string {
|
||||||
|
hd := hashids.NewData()
|
||||||
|
hd.Salt = hashIdSalt
|
||||||
|
hd.MinLength = hashIdMinLength
|
||||||
|
h, _ := hashids.NewWithData(hd)
|
||||||
|
s, _ := h.EncodeInt64([]int64{int64(v)})
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Server) AddMetadata(m Metadata) (string, error) {
|
||||||
jb, err := json.Marshal(m)
|
jb, err := json.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.db.Update(func(tx *bolt.Tx) error {
|
var shortRef string = ""
|
||||||
return tx.Bucket(this.metadataBucket).Put([]byte(fileID), jb)
|
|
||||||
|
err = this.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket(this.metadataBucket)
|
||||||
|
seq, _ := b.NextSequence() // cannot fail
|
||||||
|
shortRef = idToString(seq)
|
||||||
|
return tx.Bucket(this.metadataBucket).Put([]byte(shortRef), jb)
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if shortRef == "" {
|
||||||
|
return "", errors.New("Invalid URL generated")
|
||||||
|
}
|
||||||
|
|
||||||
|
return shortRef, nil
|
||||||
}
|
}
|
||||||
|
28
Server.go
28
Server.go
@ -5,7 +5,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
@ -56,33 +55,6 @@ func NewServer(opts *ServerOptions) (*Server, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) error {
|
|
||||||
// Load file
|
|
||||||
f, err := os.Open(filepath.Join(this.opts.DataDirectory, fileID))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// Load metadata
|
|
||||||
m, err := this.Metadata(fileID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeContent only uses the filename to get the mime type, which we can
|
|
||||||
// set accurately (including blacklist)
|
|
||||||
w.Header().Set(`Content-Type`, m.MimeType)
|
|
||||||
if m.MimeType == `application/octet-stream` {
|
|
||||||
w.Header().Set(`Content-Disposition`, `attachment; filename="`+m.Filename+`"`)
|
|
||||||
}
|
|
||||||
|
|
||||||
http.ServeContent(w, r, "", m.UploadTime, f)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Server) handleInformation(w http.ResponseWriter, fileID string) {
|
func (this *Server) handleInformation(w http.ResponseWriter, fileID string) {
|
||||||
m, err := this.Metadata(fileID)
|
m, err := this.Metadata(fileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -15,6 +15,7 @@ You can use contented as a standalone upload server, or you can use the SDK to e
|
|||||||
- Hash verification (SHA512/256)
|
- Hash verification (SHA512/256)
|
||||||
- Detect duplicate upload content and reuse storage
|
- Detect duplicate upload content and reuse storage
|
||||||
- Options to limit the upload filesize and the upload bandwidth
|
- Options to limit the upload filesize and the upload bandwidth
|
||||||
|
- Short URLs (using [url=http://hashids.org]Hashids[/url] algorithm)
|
||||||
|
|
||||||
=USAGE (SERVER)=
|
=USAGE (SERVER)=
|
||||||
|
|
||||||
@ -50,6 +51,5 @@ contented.init("#target", function(/* String[] */ items) {});
|
|||||||
|
|
||||||
- View upload history / view all uploads
|
- View upload history / view all uploads
|
||||||
- Encrypted at rest (anti- provider snooping)
|
- Encrypted at rest (anti- provider snooping)
|
||||||
- Shorter URLs
|
|
||||||
- Deploy!
|
- Deploy!
|
||||||
- Display 'my uploads' (id + metadata history kept in localstorage)
|
- Display 'my uploads' (id + metadata history kept in localstorage)
|
||||||
|
35
download.go
Normal file
35
download.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package contented
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) error {
|
||||||
|
|
||||||
|
// Load metadata
|
||||||
|
m, err := this.Metadata(fileID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load file
|
||||||
|
f, err := os.Open(filepath.Join(this.opts.DataDirectory, m.FileHash))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
// ServeContent only uses the filename to get the mime type, which we can
|
||||||
|
// set accurately (including blacklist)
|
||||||
|
w.Header().Set(`Content-Type`, m.MimeType)
|
||||||
|
if m.MimeType == `application/octet-stream` {
|
||||||
|
w.Header().Set(`Content-Disposition`, `attachment; filename="`+m.Filename+`"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.ServeContent(w, r, "", m.UploadTime, f)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
32
upload.go
32
upload.go
@ -88,20 +88,24 @@ func (this *Server) handleUploadFile(src multipart.File, hdr *multipart.FileHead
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save file to disk
|
// Save file to disk
|
||||||
fileID := hex.EncodeToString(hasher.Sum(nil))
|
fileHash := hex.EncodeToString(hasher.Sum(nil))
|
||||||
dest, err := os.OpenFile(filepath.Join(this.opts.DataDirectory, fileID), os.O_CREATE|os.O_WRONLY, 0600)
|
dest, err := os.OpenFile(filepath.Join(this.opts.DataDirectory, fileHash), os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
shouldSave := true
|
||||||
if os.IsExist(err) {
|
if err != nil && os.IsExist(err) {
|
||||||
return fileID, nil // hash matches existing upload
|
// hash matches existing upload
|
||||||
}
|
// That's fine - but still persist the metadata separately
|
||||||
|
shouldSave = false
|
||||||
|
} else if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer dest.Close()
|
|
||||||
|
|
||||||
_, err = io.CopyN(dest, src, int64(srcLen))
|
if shouldSave {
|
||||||
if err != nil {
|
defer dest.Close()
|
||||||
return "", err
|
|
||||||
|
_, err = io.CopyN(dest, src, int64(srcLen))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine mime type
|
// Determine mime type
|
||||||
@ -122,17 +126,19 @@ func (this *Server) handleUploadFile(src multipart.File, hdr *multipart.FileHead
|
|||||||
|
|
||||||
// Persist metadata to DB
|
// Persist metadata to DB
|
||||||
m := Metadata{
|
m := Metadata{
|
||||||
|
FileHash: fileHash,
|
||||||
Filename: hdr.Filename,
|
Filename: hdr.Filename,
|
||||||
UploadTime: time.Now(),
|
UploadTime: time.Now(),
|
||||||
UploadIP: UploadIP,
|
UploadIP: UploadIP,
|
||||||
FileSize: srcLen,
|
FileSize: srcLen,
|
||||||
MimeType: ctype,
|
MimeType: ctype,
|
||||||
}
|
}
|
||||||
err = this.SetMetadata(fileID, m)
|
|
||||||
|
fileRef, err := this.AddMetadata(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
return fileID, nil
|
return fileRef, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user