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 (
"io"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"git.t-juice.club/torjus/gpaste/files"
"git.t-juice.club/torjus/gpaste/users"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
@ -14,7 +15,7 @@ import (
)
type HTTPServer struct {
Files FileStore
Files files.FileStore
Users users.UserStore
Auth *AuthService
config *ServerConfig
@ -29,7 +30,7 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
Logger: zap.NewNop().Sugar(),
AccessLogger: zap.NewNop().Sugar(),
}
srv.Files = NewMemoryFileStore()
srv.Files = files.NewMemoryFileStore()
srv.Users = users.NewMemoryUserStore()
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) {
f := &File{
f := &files.File{
ID: uuid.Must(uuid.NewRandom()).String(),
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)
return
}
f := &File{
f := &files.File{
ID: uuid.Must(uuid.NewRandom()).String(),
OriginalFilename: fh.Filename,
Body: ff,