Skip to content

Commit 5424f35

Browse files
committed
Calculate and store filesize on file uploads
1 parent ce6cd9a commit 5424f35

File tree

9 files changed

+133
-89
lines changed

9 files changed

+133
-89
lines changed

pkg/cli/serve/demo.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func setupDemo() {
5151
},
5252
},
5353
})
54-
ref, err := d.CreateFile([]byte("This is an example of an uploaded file"))
54+
ref, size, err := d.CreateFile([]byte("This is an example of an uploaded file"))
5555
if err != nil {
5656
log.Errorf("Unable to upload example file: %s", err)
5757
}
@@ -60,6 +60,7 @@ func setupDemo() {
6060
Content: &pb.ExpandedURL_File{
6161
File: &pb.Upload{
6262
Ref: ref,
63+
Size: size,
6364
},
6465
},
6566
},

server/gen/shrls.pb.go

+108-82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/gen/shrls.swagger.json

+9
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@
317317
"properties": {
318318
"id": {
319319
"type": "string"
320+
},
321+
"size": {
322+
"type": "string",
323+
"format": "int64"
320324
}
321325
}
322326
},
@@ -536,6 +540,11 @@
536540
"ref": {
537541
"type": "string",
538542
"title": "Reference to pass to storage backend to find uploaded file"
543+
},
544+
"size": {
545+
"type": "string",
546+
"format": "int64",
547+
"title": "File size in bytes"
539548
}
540549
},
541550
"title": "File Upload"

server/proto/shrls.proto

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ message Ref {
129129
message FileUpload {
130130
oneof ref {
131131
string id = 1;
132+
int64 size = 2;
132133
}
133134
}
134135
}
@@ -169,6 +170,7 @@ message Redirect {
169170
// File Upload
170171
message Upload {
171172
string ref = 1; // Reference to pass to storage backend to find uploaded file
173+
int64 size = 2; // File size in bytes
172174
}
173175

174176
message ExpandedURL {

server/storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package server
22

33
type ServerStorage interface {
4-
CreateFile([]byte) (string, error)
4+
CreateFile([]byte) (string, int64, error)
55
ReadFile(string) ([]byte, error)
66
DeleteFile(string) error
77
}

service/fileupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func (s Server) PostFileUpload(ctx context.Context, req *pb.PostFileUploadRequest) (*pb.PostFileUploadResponse, error) {
1010
file := req.File
11-
key, err := s.storage.CreateFile(file)
11+
key, _, err := s.storage.CreateFile(file)
1212
if err != nil {
1313
return nil, err
1414
}

state/boltdb/shrl.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type URL struct {
2727
Alias string
2828
Location string
2929
UploadLocation string
30+
UploadSize int64
3031
SnippetTitle string
3132
Snippet string
3233
CreatedAt time.Time `storm:"index"`
@@ -68,6 +69,7 @@ func (s *BoltDBState) pbShrlToUrl(in *pb.ShortURL) *URL {
6869
case *pb.ExpandedURL_File:
6970
out.Type = UploadedFile
7071
out.UploadLocation = in.Content.GetFile().GetRef()
72+
out.UploadSize = in.Content.GetFile().GetSize()
7173
case *pb.ExpandedURL_Snippet:
7274
out.Type = TextSnippet
7375
out.Snippet = string(in.Content.GetSnippet().GetBody())
@@ -100,6 +102,7 @@ func (s *BoltDBState) urlToPbShrl(in *URL) *pb.ShortURL {
100102
Content: &pb.ExpandedURL_File{
101103
File: &pb.Upload{
102104
Ref: in.UploadLocation,
105+
Size: in.UploadSize,
103106
},
104107
},
105108
}

state/mongo/shrl.go

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type URL struct {
3333
Alias string `bson:"alias" json:"alias"`
3434
Location string `bson:"location" json:"location"`
3535
UploadLocation string `bson:"upload_location" json:"-"`
36+
UploadSize int64 `bson:"upload_size" json:"-"`
3637
SnippetTitle string `bson:"snippet_title" json:"snippet_title,omitempty"`
3738
Snippet string `bson:"snippet" json:"snippet,omitempty"`
3839
CreatedAt time.Time `bson:"created_at" json:"created_at"`
@@ -79,6 +80,7 @@ func (s *MongoDBState) urlToPbShrl(u *URL) *pb.ShortURL {
7980
Content: &pb.ExpandedURL_File{
8081
File: &pb.Upload{
8182
Ref: u.UploadLocation,
83+
Size: u.UploadSize,
8284
},
8385
},
8486
}
@@ -129,6 +131,7 @@ func (s *MongoDBState) pbShrlToUrl(u *pb.ShortURL) *URL {
129131
case *pb.ExpandedURL_File:
130132
url.Type = UploadedFile
131133
url.UploadLocation = u.Content.GetFile().GetRef()
134+
url.UploadSize = u.Content.GetFile().GetSize()
132135
case *pb.ExpandedURL_Snippet:
133136
url.Type = TextSnippet
134137
url.Snippet = string(u.Content.GetSnippet().GetBody())

storage/directory/directory.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ type DirectoryStorage struct {
1414
upload_location string
1515
}
1616

17-
func (d *DirectoryStorage) CreateFile(file []byte) (string, error) {
17+
func (d *DirectoryStorage) CreateFile(file []byte) (string, int64, error) {
1818
err := os.MkdirAll(d.upload_location, os.ModePerm)
1919
if err != nil {
20-
return "", err
20+
return "", 0, err
2121
}
2222

2323
key := uuid.New()
2424
filepath := path.Join(d.upload_location, key.String())
2525

2626
err = os.WriteFile(filepath, file, os.ModePerm)
2727
if err != nil {
28-
return "", err
28+
return "", 0, err
2929
}
3030

31-
return key.String(), nil
31+
return key.String(), int64(len(file)), nil
3232
}
3333

3434
func (d *DirectoryStorage) ReadFile(key string) ([]byte, error) {

0 commit comments

Comments
 (0)