Skip to content

Commit

Permalink
Add html templates
Browse files Browse the repository at this point in the history
  • Loading branch information
espebra committed Apr 4, 2020
1 parent e8344bd commit d897762
Show file tree
Hide file tree
Showing 34 changed files with 10,111 additions and 69 deletions.
23 changes: 23 additions & 0 deletions dbl/bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/espebra/filebin2/ds"
"math/rand"
"time"
)

type BinDao struct {
db *sql.DB
}

func (d *BinDao) GenerateId() string {
// TODO: Make sure the ID is unique
characters := []rune("abcdefghijklmnopqrstuvwxyz1234567890")
length := 10

id := make([]rune, length)
for i := range id {
id[i] = characters[rand.Intn(len(characters))]
}
return string(id)
}

func (d *BinDao) GetAll() ([]ds.Bin, error) {
bins := []ds.Bin{}

Expand Down Expand Up @@ -73,6 +86,11 @@ func (d *BinDao) GetById(id string) (ds.Bin, error) {

func (d *BinDao) Upsert(bin *ds.Bin) error {
now := time.Now().UTC().Truncate(time.Microsecond)

if bin.Id == "" {
bin.Id = d.GenerateId()
}

sqlStatement := "SELECT bin.id, bin.downloads, COALESCE(SUM(file.bytes), 0), bin.updated, bin.created, bin.expiration FROM bin LEFT JOIN file ON bin.id = file.bin_id WHERE bin.id = $1 GROUP BY bin.id LIMIT 1"
err := d.db.QueryRow(sqlStatement, bin.Id).Scan(&bin.Id, &bin.Downloads, &bin.Bytes, &bin.Updated, &bin.Created, &bin.Expiration)
if err != nil {
Expand Down Expand Up @@ -104,6 +122,11 @@ func (d *BinDao) Upsert(bin *ds.Bin) error {
func (d *BinDao) Insert(bin *ds.Bin) error {
now := time.Now().UTC().Truncate(time.Microsecond)
expiration := now.Add(time.Hour * 24 * 7)

if bin.Id == "" {
bin.Id = d.GenerateId()
}

sqlStatement := "INSERT INTO bin (id, downloads, updated, created, expiration) VALUES ($1, 0, $2, $3, $4) RETURNING id"
err := d.db.QueryRow(sqlStatement, bin.Id, now, now, expiration).Scan(&bin.Id)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func (h *HTTP) Init() (err error) {

h.router.HandleFunc("/", h.Index).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/", h.Upload).Methods(http.MethodPost)
h.router.Handle("/static/{path:.*}", http.StripPrefix("/static/", http.FileServer(h.staticBox.HTTPBox()))).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/{bin:[A-Za-z0-9_-]+}", h.ViewBin).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/{bin:[A-Za-z0-9_-]+}/{filename:.+}", h.GetFile).Methods(http.MethodHead, http.MethodGet)
h.router.Handle("/static/{path:.*}", http.StripPrefix("/static/", http.FileServer(h.staticBox.HTTPBox()))).Methods(http.MethodHead, http.MethodGet)
return err
}

Expand Down
14 changes: 9 additions & 5 deletions http_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (h *HTTP) GetFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Expires", bin.Expiration.Format(http.TimeFormat))
w.Header().Set("Last-Modified", file.Updated.Format(http.TimeFormat))
w.Header().Set("Bin", file.Bin)
w.Header().Set("Filebin", file.Filename)
w.Header().Set("Filename", file.Filename)
//w.Header().Set("Cache-Control", "s-maxage=1")

w.Header().Set("Content-Length", fmt.Sprintf("%d", file.Bytes))
Expand Down Expand Up @@ -158,14 +158,16 @@ func (h *HTTP) Upload(w http.ResponseWriter, r *http.Request) {
return
}

t1 := time.Now()

nBytes, err := io.Copy(fp, r.Body)
if err != nil {
fmt.Printf("Error occurred during io.Copy: %s\n", err.Error())
return
}
fp.Seek(0, 0)

t1 := time.Now()
t2 := time.Now()

checksum := md5.New()
if _, err := io.Copy(checksum, fp); err != nil {
Expand All @@ -192,15 +194,15 @@ func (h *HTTP) Upload(w http.ResponseWriter, r *http.Request) {
file.Mime = mime.String()
fp.Seek(0, 0)

t2 := time.Now()
t3 := time.Now()

//fmt.Printf("Buffered %s to %s in %.3fs\n", humanize.Bytes(nBytes), fp.Name(), time.Since(start).Seconds())

nonce, err := h.s3.PutObject(file.Bin, file.Filename, fp, nBytes)
if err != nil {
fmt.Printf("%s\n", err.Error())
}
t3 := time.Now()
t4 := time.Now()

file.Nonce = nonce
if err := h.dao.File().Update(file); err != nil {
Expand All @@ -210,5 +212,7 @@ func (h *HTTP) Upload(w http.ResponseWriter, r *http.Request) {
}

//fmt.Printf("Uploaded %s to S3 in %.3fs\n", humanize.Bytes(nBytes), time.Since(start).Seconds())
fmt.Printf("Uploaded filename %s (%s) to bin %s (buffered in %.3fs, checksum in %.3fs, stored in %.3fs)\n", inputFilename, humanize.Bytes(inputBytes), inputBin, t1.Sub(t0).Seconds(), t2.Sub(t1).Seconds(), t3.Sub(t2).Seconds())
fmt.Printf("Uploaded filename %s (%s) to bin %s (db in %.3fs, buffered in %.3fs, checksum in %.3fs, stored in %.3fs, total %.3fs)\n", inputFilename, humanize.Bytes(inputBytes), inputBin, t1.Sub(t0).Seconds(), t2.Sub(t1).Seconds(), t3.Sub(t2).Seconds(), t4.Sub(t3).Seconds(), t4.Sub(t0).Seconds())

w.WriteHeader(http.StatusCreated)
}
11 changes: 11 additions & 0 deletions http_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package main

import (
"fmt"
"github.com/espebra/filebin2/ds"
"net/http"
)

func (h *HTTP) Index(w http.ResponseWriter, r *http.Request) {

type Data struct {
Bin ds.Bin `json:"bin"`
}
var data Data

bin := &ds.Bin{}
err := h.dao.Bin().Insert(bin)
if err != nil {
fmt.Printf("Unable to insert new bin: %s\n", err.Error())
http.Error(w, "Errno 5", http.StatusInternalServerError)
return
}
data.Bin = *bin

if err := h.templates.ExecuteTemplate(w, "index", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 1", http.StatusInternalServerError)
Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/GeertJohan/go.rice"
"github.com/espebra/filebin2/dbl"
"github.com/espebra/filebin2/s3"
"math/rand"
"time"
)

var (
Expand All @@ -32,20 +34,23 @@ var (
)

func main() {
rand.Seed(time.Now().UTC().UnixNano())
flag.Parse()

daoconn, err := dbl.Init(*dbHostFlag, *dbPortFlag, *dbNameFlag, *dbUsernameFlag, *dbPasswordFlag)
if err != nil {
fmt.Errorf("Unable to connect to the database: %s\n", err.Error())
fmt.Printf("Unable to connect to the database: %s\n", err.Error())
os.Exit(2)
}

if err := daoconn.CreateSchema(); err != nil {
fmt.Errorf("Unable to create Schema: %s\n", err.Error())
fmt.Printf("Unable to create Schema: %s\n", err.Error())
}

s3conn, err := s3.Init(*s3EndpointFlag, *s3BucketFlag, *s3RegionFlag, *s3AccessKeyFlag, *s3SecretKeyFlag, *s3EncryptionKeyFlag)
if err != nil {
fmt.Errorf("Unable to connect to S3: %s\n", err.Error())
fmt.Printf("Unable to connect to S3: %s\n", err.Error())
os.Exit(2)
}

staticBox := rice.MustFindBox("static")
Expand All @@ -61,7 +66,7 @@ func main() {
}

if err := h.Init(); err != nil {
fmt.Errorf("Unable to start the HTTP server: %s\n", err.Error())
fmt.Printf("Unable to start the HTTP server: %s\n", err.Error())
}

h.Run()
Expand Down
7 changes: 7 additions & 0 deletions static/css/bootstrap.min.css

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions static/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.nav-pills>li+li {
margin-left: 10px; // 2px by default
}

.thumbnails {
margin-top: 10px;
}

.table {
margin-top: 10px;
}

.thumb {
padding: 1px 1px 1px 1px;
}

.dark-background {
background-color: #BBBBBB;
}

.progress {
margin-bottom: 0 !important;
}

.fileUpload {
position: relative;
overflow: hidden;
}
.fileUpload input.upload {
position: absolute;
top: 0;
right: 0;
margin: 0;
padding: 0;
font-size: 20px;
cursor: pointer;
opacity: 0;
filter: alpha(opacity=0);
}
5 changes: 5 additions & 0 deletions static/css/fontawesome.all.min.css

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions static/css/upload.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
html, body {
margin: 0;
height: 100%;
}

.hide {
display: none;
position: relative;
}

#fileCount {
display: none;
position: relative;
}

#files {
margin-top: 30px;
min-height: 30px;
}

#files {
-moz-box-shadow: 0 0 20px #ccc;
}

#fileList {
list-style: none;
padding: 0;
margin-bottom: 30px;
margin-left: 0px;
}

#fileList li {
margin: 0;
padding-top: 15px;
overflow: auto;
position: relative;
}


.loader {
position: absolute;
bottom: 10px;
right: 0;
color: orange;
}

.loadingIndicator {
width: 0%;
height: 2px;
background-color: #c09853;
position: absolute;
bottom: 0;
left: 0;
}
7 changes: 7 additions & 0 deletions static/js/bootstrap.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions static/js/jquery-3.4.1.slim.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions static/js/popper.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit d897762

Please sign in to comment.