Create files package
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-20 03:40:32 +01:00
parent d4b7702bad
commit faa3cc102f
7 changed files with 24 additions and 23 deletions

View File

@ -1,4 +1,4 @@
package gpaste package files
import ( import (
"io" "io"

View File

@ -1,4 +1,4 @@
package gpaste package files
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,22 +1,22 @@
package gpaste_test package files_test
import ( import (
"testing" "testing"
"git.t-juice.club/torjus/gpaste" "git.t-juice.club/torjus/gpaste/files"
) )
func TestFSFileStore(t *testing.T) { func TestFSFileStore(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
s, err := gpaste.NewFSFileStore(dir) s, err := files.NewFSFileStore(dir)
if err != nil { if err != nil {
t.Fatalf("Error creating store: %s", err) t.Fatalf("Error creating store: %s", err)
} }
RunFilestoreTest(s, t) RunFilestoreTest(s, t)
persistentDir := t.TempDir() persistentDir := t.TempDir()
newFunc := func() gpaste.FileStore { newFunc := func() files.FileStore {
s, err := gpaste.NewFSFileStore(persistentDir) s, err := files.NewFSFileStore(persistentDir)
if err != nil { if err != nil {
t.Fatalf("Error creating store: %s", err) t.Fatalf("Error creating store: %s", err)
} }

View File

@ -1,4 +1,4 @@
package gpaste package files
import ( import (
"bytes" "bytes"

View File

@ -1,13 +1,13 @@
package gpaste_test package files_test
import ( import (
"testing" "testing"
"git.t-juice.club/torjus/gpaste" "git.t-juice.club/torjus/gpaste/files"
) )
func TestMemoryFileStore(t *testing.T) { func TestMemoryFileStore(t *testing.T) {
s := gpaste.NewMemoryFileStore() s := files.NewMemoryFileStore()
RunFilestoreTest(s, t) RunFilestoreTest(s, t)
} }

View File

@ -1,4 +1,4 @@
package gpaste_test package files_test
import ( import (
"bytes" "bytes"
@ -7,12 +7,12 @@ import (
"testing" "testing"
"time" "time"
"git.t-juice.club/torjus/gpaste" "git.t-juice.club/torjus/gpaste/files"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/google/uuid" "github.com/google/uuid"
) )
func RunFilestoreTest(s gpaste.FileStore, t *testing.T) { func RunFilestoreTest(s files.FileStore, t *testing.T) {
t.Run("Basic", func(t *testing.T) { t.Run("Basic", func(t *testing.T) {
// Create // Create
dataString := "TEST_LOL_OMG" dataString := "TEST_LOL_OMG"
@ -20,7 +20,7 @@ func RunFilestoreTest(s gpaste.FileStore, t *testing.T) {
bodyBuf := &bytes.Buffer{} bodyBuf := &bytes.Buffer{}
bodyBuf.Write([]byte(dataString)) bodyBuf.Write([]byte(dataString))
body := io.NopCloser(bodyBuf) body := io.NopCloser(bodyBuf)
f := &gpaste.File{ f := &files.File{
ID: id, ID: id,
MaxViews: 0, MaxViews: 0,
Body: body, Body: body,
@ -78,15 +78,15 @@ func RunFilestoreTest(s gpaste.FileStore, t *testing.T) {
}) })
} }
func RunPersistentFilestoreTest(newStoreFunc func() gpaste.FileStore, t *testing.T) { func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) {
s := newStoreFunc() s := newStoreFunc()
files := []struct { files := []struct {
File *gpaste.File File *files.File
ExpectedData string ExpectedData string
}{ }{
{ {
File: &gpaste.File{ File: &files.File{
ID: uuid.NewString(), ID: uuid.NewString(),
OriginalFilename: "testfile.txt", OriginalFilename: "testfile.txt",
MaxViews: 5, MaxViews: 5,
@ -96,7 +96,7 @@ func RunPersistentFilestoreTest(newStoreFunc func() gpaste.FileStore, t *testing
ExpectedData: "cocks!", ExpectedData: "cocks!",
}, },
{ {
File: &gpaste.File{ File: &files.File{
ID: uuid.NewString(), ID: uuid.NewString(),
OriginalFilename: "testfile2.txt", OriginalFilename: "testfile2.txt",
MaxViews: 5, MaxViews: 5,

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.t-juice.club/torjus/gpaste/files"
"git.t-juice.club/torjus/gpaste/users" "git.t-juice.club/torjus/gpaste/users"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
@ -14,7 +15,7 @@ import (
) )
type HTTPServer struct { type HTTPServer struct {
Files FileStore Files files.FileStore
Users users.UserStore Users users.UserStore
Auth *AuthService Auth *AuthService
config *ServerConfig config *ServerConfig
@ -29,7 +30,7 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
Logger: zap.NewNop().Sugar(), Logger: zap.NewNop().Sugar(),
AccessLogger: zap.NewNop().Sugar(), AccessLogger: zap.NewNop().Sugar(),
} }
srv.Files = NewMemoryFileStore() srv.Files = files.NewMemoryFileStore()
srv.Users = users.NewMemoryUserStore() srv.Users = users.NewMemoryUserStore()
srv.Auth = NewAuthService(srv.Users, []byte(srv.config.SigningSecret)) srv.Auth = NewAuthService(srv.Users, []byte(srv.config.SigningSecret))
@ -59,7 +60,7 @@ func (s *HTTPServer) HandlerIndex(w http.ResponseWriter, r *http.Request) {
} }
func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request) { func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request) {
f := &File{ f := &files.File{
ID: uuid.Must(uuid.NewRandom()).String(), ID: uuid.Must(uuid.NewRandom()).String(),
Body: r.Body, Body: r.Body,
} }
@ -134,7 +135,7 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err) s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err)
return return
} }
f := &File{ f := &files.File{
ID: uuid.Must(uuid.NewRandom()).String(), ID: uuid.Must(uuid.NewRandom()).String(),
OriginalFilename: fh.Filename, OriginalFilename: fh.Filename,
Body: ff, Body: ff,