diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c32b900 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/Frontend/public/images/* diff --git a/Api/PostHelper.go b/Api/PostHelper.go new file mode 100644 index 0000000..0f55c75 --- /dev/null +++ b/Api/PostHelper.go @@ -0,0 +1,50 @@ +package Api + +import ( + "errors" + "log" + "net/http" + + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" + "github.com/gorilla/mux" +) + +func getPostId(r *http.Request) (string, error) { + var ( + urlVars map[string]string + id string + ok bool + ) + + urlVars = mux.Vars(r) + id, ok = urlVars["postID"] + if !ok { + return id, errors.New("Could not get id") + } + return id, nil +} + +func getPostById(w http.ResponseWriter, r *http.Request) (Models.Post, error) { + var ( + postData Models.Post + id string + err error + ) + + id, err = getPostId(r) + if err != nil { + log.Printf("Error encountered getting id\n") + JsonReturn(w, 500, "An error occured") + return postData, err + } + + postData, err = Database.GetPostById(id) + if err != nil { + log.Printf("Could not find pet with id %s\n", id) + JsonReturn(w, 404, "Not found") + return postData, err + } + + return postData, nil +} diff --git a/Api/PostImageHelper.go b/Api/PostImageHelper.go new file mode 100644 index 0000000..06142c4 --- /dev/null +++ b/Api/PostImageHelper.go @@ -0,0 +1,50 @@ +package Api + +import ( + "errors" + "log" + "net/http" + + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" + "github.com/gorilla/mux" +) + +func getPostImageId(r *http.Request) (string, error) { + var ( + urlVars map[string]string + id string + ok bool + ) + + urlVars = mux.Vars(r) + id, ok = urlVars["imageID"] + if !ok { + return id, errors.New("Could not get id") + } + return id, nil +} + +func getPostImageById(w http.ResponseWriter, r *http.Request) (Models.PostImage, error) { + var ( + postImageData Models.PostImage + id string + err error + ) + + id, err = getPostImageId(r) + if err != nil { + log.Printf("Error encountered getting id\n") + JsonReturn(w, 500, "An error occured") + return postImageData, err + } + + postImageData, err = Database.GetPostImageById(id) + if err != nil { + log.Printf("Could not find pet with id %s\n", id) + JsonReturn(w, 404, "Not found") + return postImageData, err + } + + return postImageData, nil +} diff --git a/Api/PostImages.go b/Api/PostImages.go new file mode 100644 index 0000000..c9c98df --- /dev/null +++ b/Api/PostImages.go @@ -0,0 +1,127 @@ +package Api + +import ( + "encoding/json" + "io/ioutil" + "log" + "mime/multipart" + "net/http" + + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util" + "github.com/gofrs/uuid" +) + +func createPostImage(w http.ResponseWriter, r *http.Request) { + var ( + postData Models.Post + postID string + postUUID uuid.UUID + postImage Models.PostImage + formData *multipart.Form + fileHeaders []*multipart.FileHeader + fileHeader *multipart.FileHeader + file multipart.File + fileBytes []byte + fileObject Util.FileObject + returnJson []byte + err error + ) + + postID, err = getPostId(r) + if err != nil { + log.Printf("Error encountered getting id\n") + JsonReturn(w, 500, "An error occured") + return + } + + postUUID = uuid.FromStringOrNil(postID) + + // TODO: Change arg to bitwise op + err = r.ParseMultipartForm(200000) + if err != nil { + log.Printf("Error encountered parsing multipart form: %s\n", err.Error()) + JsonReturn(w, 500, "An error occured") + return + } + + formData = r.MultipartForm + + fileHeaders = formData.File["files"] + + for _, fileHeader = range fileHeaders { + file, err = fileHeader.Open() + if err != nil { + log.Printf("Error encountered while post image upload: %s\n", err.Error()) + JsonReturn(w, 500, "An error occured") + return + } + defer file.Close() + + fileBytes, err = ioutil.ReadAll(file) + if err != nil { + log.Printf("Error encountered while post image upload: %s\n", err.Error()) + JsonReturn(w, 500, "An error occured") + return + } + + fileObject, err = Util.WriteFile(fileBytes, "image") + if err != nil { + log.Printf("Error encountered while post image upload: %s\n", err.Error()) + JsonReturn(w, 415, "Invalid filetype") + return + } + + postImage = Models.PostImage{ + PostID: postUUID, + Filepath: fileObject.Filepath, + Mimetype: fileObject.Mimetype, + Size: fileObject.Size, + } + + err = Database.CreatePostImage(&postImage) + if err != nil { + log.Printf("Error encountered while creating post_image record: %s\n", err.Error()) + JsonReturn(w, 500, "An error occured") + return + } + } + + postData, err = getPostById(w, r) + if err != nil { + return + } + + returnJson, err = json.MarshalIndent(postData, "", " ") + if err != nil { + JsonReturn(w, 500, "An error occured") + return + } + + // Return updated json + w.WriteHeader(http.StatusOK) + w.Write(returnJson) +} + +func deletePostImage(w http.ResponseWriter, r *http.Request) { + var ( + postImageData Models.PostImage + err error + ) + + postImageData, err = getPostImageById(w, r) + if err != nil { + return + } + + err = Database.DeletePostImage(&postImageData) + if err != nil { + log.Printf("An error occured: %s\n", err.Error()) + JsonReturn(w, 500, "An error occured") + return + } + + // Return updated json + w.WriteHeader(http.StatusOK) +} diff --git a/Api/PostImages_test.go b/Api/PostImages_test.go new file mode 100644 index 0000000..80b3fa9 --- /dev/null +++ b/Api/PostImages_test.go @@ -0,0 +1,273 @@ +package Api + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "io/ioutil" + "log" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "path" + "path/filepath" + "runtime" + "strconv" + "testing" + + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" + "gorm.io/gorm" + + "github.com/gorilla/mux" +) + +func init() { + // Fix working directory for tests + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "..") + err := os.Chdir(dir) + if err != nil { + panic(err) + } + + log.SetOutput(ioutil.Discard) + Database.Init() + + r = mux.NewRouter() +} + +func get(url string) (resp *http.Response, err error) { + for i := 0; i < 5; i++ { + resp, err = http.Get(url) + if err == nil { + return resp, err + } + } + + return resp, err +} + +// Image generates a *os.File with a random image using the loremflickr.com service +func fakeImage(width, height int, categories []string, prefix string, categoriesStrict bool) *os.File { + url := "https://loremflickr.com" + + switch prefix { + case "g": + url += "/g" + case "p": + url += "/p" + case "red": + url += "/red" + case "green": + url += "/green" + case "blue": + url += "/blue" + } + + url += string('/') + strconv.Itoa(width) + string('/') + strconv.Itoa(height) + + if len(categories) > 0 { + + url += string('/') + + for _, category := range categories { + url += category + string(',') + } + + if categoriesStrict { + url += "/all" + } + } + + resp, err := get(url) + defer resp.Body.Close() + + if err != nil { + panic(err) + } + + f, err := ioutil.TempFile(os.TempDir(), "loremflickr-img-*.jpg") + if err != nil { + panic(err) + } + + io.Copy(f, resp.Body) + + err = f.Close() + if err != nil { + panic(err) + } + + return f +} + +func Test_createPostImages(t *testing.T) { + t.Log("Testing createPostImages...") + + r.HandleFunc("/post/{postID}/image", createPostImage).Methods("POST") + + ts := httptest.NewServer(r) + defer ts.Close() + + postData := Models.Post{ + Title: "Test post", + Content: "Test content", + FrontPage: true, + Order: 1, + PostLinks: []Models.PostLink{ + { + Type: "Facebook", + Link: "http://facebook.com/", + }, + }, + } + + err := Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + image := fakeImage(100, 100, []string{}, "", false) + + image, err = os.Open(image.Name()) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("files", filepath.Base(image.Name())) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + io.Copy(part, image) + writer.Close() + request, err := http.NewRequest( + "POST", + fmt.Sprintf( + "%s/post/%s/image", + ts.URL, + postData.ID, + ), + body, + ) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + request.Header.Add("Content-Type", writer.FormDataContentType()) + client := &http.Client{} + + res, err := client.Do(request) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + if res.StatusCode != http.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) + } + + defer res.Body.Close() + + updatePostData := new(Models.Post) + err = json.NewDecoder(res.Body).Decode(updatePostData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks", "PostImages"). + Delete(&postData) + + if len(updatePostData.PostImages) != 1 { + t.Errorf("Expected len(updatePostData.PostImages) == 1, recieved %d", len(updatePostData.PostImages)) + } + + for _, f := range updatePostData.PostImages { + if _, err := os.Stat("./" + f.Filepath); errors.Is(err, os.ErrNotExist) { + t.Errorf( + "File ./%s does not exist", + f.Filepath, + ) + } else { + os.Remove("./" + f.Filepath) + } + } +} + +func Test_deletePostImages(t *testing.T) { + t.Log("Testing createPostImages...") + + r.HandleFunc("/post/{postID}/image/{imageID}", deletePostImage).Methods("DELETE") + + ts := httptest.NewServer(r) + defer ts.Close() + + image := fakeImage(100, 100, []string{}, "", false) + + image, err := os.Open(image.Name()) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + defer image.Close() + + postData := Models.Post{ + Title: "Test post", + Content: "Test content", + FrontPage: true, + Order: 1, + PostLinks: []Models.PostLink{ + { + Type: "Facebook", + Link: "http://facebook.com/", + }, + }, + PostImages: []Models.PostImage{ + { + Filepath: image.Name(), + }, + }, + } + + err = Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks", "PostImages"). + Delete(&postData) + + req, err := http.NewRequest("DELETE", fmt.Sprintf( + "%s/post/%s/image/%s", + ts.URL, + postData.ID, + postData.PostImages[0].ID, + ), nil) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) + } +} diff --git a/Api/Posts.go b/Api/Posts.go index c2752b9..47a8988 100644 --- a/Api/Posts.go +++ b/Api/Posts.go @@ -2,7 +2,6 @@ package Api import ( "encoding/json" - "errors" "io/ioutil" "log" "net/http" @@ -15,33 +14,6 @@ import ( "github.com/gorilla/mux" ) -func getPostById(w http.ResponseWriter, r *http.Request) (Models.Post, error) { - var ( - postData Models.Post - urlVars map[string]string - id string - ok bool - err error - ) - - urlVars = mux.Vars(r) - id, ok = urlVars["postID"] - if !ok { - log.Printf("Error encountered getting id\n") - JsonReturn(w, 500, "An error occured") - return postData, errors.New("Could not get id") - } - - postData, err = Database.GetPostById(id) - if err != nil { - log.Printf("Could not find pet with id %s\n", id) - JsonReturn(w, 404, "Not found") - return postData, err - } - - return postData, nil -} - func getPosts(w http.ResponseWriter, r *http.Request) { var ( posts []Models.Post diff --git a/Api/Posts_test.go b/Api/Posts_test.go index 2493e80..3f2feb7 100644 --- a/Api/Posts_test.go +++ b/Api/Posts_test.go @@ -7,12 +7,16 @@ import ( "log" "net/http" "net/http/httptest" + "os" + "path" + "runtime" "strings" "testing" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" "github.com/gorilla/mux" + "gorm.io/gorm" ) var ( @@ -20,6 +24,14 @@ var ( ) func init() { + // Fix working directory for tests + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "..") + err := os.Chdir(dir) + if err != nil { + panic(err) + } + log.SetOutput(ioutil.Discard) Database.Init() @@ -27,11 +39,14 @@ func init() { } func Test_getPosts(t *testing.T) { + t.Log("Testing getPosts...") + r.HandleFunc("/post", getPosts).Methods("GET") ts := httptest.NewServer(r) defer ts.Close() + var err error for i := 0; i < 20; i++ { postData := Models.Post{ Title: "Test post", @@ -41,14 +56,21 @@ func Test_getPosts(t *testing.T) { PostLinks: []Models.PostLink{ { Type: "Facebook", - Link: "http://google.com/", + Link: "http://facebook.com/", }, }, } - Database.CreatePost(&postData) + err = Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } - defer Database.DB.Unscoped().Delete(&postData) + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks"). + Delete(&postData) } res, err := http.Get(ts.URL + "/post?page=1&pageSize=10") @@ -72,6 +94,8 @@ func Test_getPosts(t *testing.T) { } func Test_getPost(t *testing.T) { + t.Log("Testing getPost...") + r.HandleFunc("/post/{postID}", getPost).Methods("GET") ts := httptest.NewServer(r) @@ -86,13 +110,21 @@ func Test_getPost(t *testing.T) { PostLinks: []Models.PostLink{ { Type: "Facebook", - Link: "http://google.com/", + Link: "http://facebook.com/", }, }, } - Database.CreatePost(&postData) - defer Database.DB.Unscoped().Delete(&postData) + err := Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks"). + Delete(&postData) res, err := http.Get(fmt.Sprintf( "%s/post/%s", @@ -115,6 +147,8 @@ func Test_getPost(t *testing.T) { } func Test_createPost(t *testing.T) { + t.Log("Testing createPost...") + r.HandleFunc("/post", createPost).Methods("POST") ts := httptest.NewServer(r) @@ -129,7 +163,7 @@ func Test_createPost(t *testing.T) { "order": 1, "links": [{ "type": "Facebook", - "link": "http://google.com/" + "link": "http://facebook.com/" }] } ` @@ -148,7 +182,11 @@ func Test_createPost(t *testing.T) { t.Errorf("Expected nil, recieved %s", err.Error()) } - defer Database.DB.Unscoped().Delete(&postData) + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks"). + Delete(&postData) if postData.Title != "Test post" { t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title) @@ -159,6 +197,8 @@ func Test_createPost(t *testing.T) { } func Test_deletePost(t *testing.T) { + t.Log("Testing deletePost...") + r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") ts := httptest.NewServer(r) @@ -173,13 +213,21 @@ func Test_deletePost(t *testing.T) { PostLinks: []Models.PostLink{ { Type: "Facebook", - Link: "http://google.com/", + Link: "http://facebook.com/", }, }, } - Database.CreatePost(&postData) - defer Database.DB.Unscoped().Delete(&postData) + err := Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks"). + Delete(&postData) req, err := http.NewRequest("DELETE", fmt.Sprintf( "%s/post/%s", @@ -205,6 +253,8 @@ func Test_deletePost(t *testing.T) { } func Test_updatePost(t *testing.T) { + t.Log("Testing updatePost...") + r.HandleFunc("/post/{postID}", updatePost).Methods("PUT") ts := httptest.NewServer(r) @@ -219,12 +269,15 @@ func Test_updatePost(t *testing.T) { PostLinks: []Models.PostLink{ { Type: "Facebook", - Link: "http://google.com/", + Link: "http://facebook.com/", }, }, } - Database.CreatePost(&postData) + err := Database.CreatePost(&postData) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } postJson := ` { @@ -261,7 +314,11 @@ func Test_updatePost(t *testing.T) { t.Errorf("Expected nil, recieved %s", err.Error()) } - defer Database.DB.Unscoped().Delete(&postData) + defer Database.DB. + Session(&gorm.Session{FullSaveAssociations: true}). + Unscoped(). + Select("PostLinks"). + Delete(&postData) if updatePostData.Content != "New test content" { t.Errorf("Expected \"New test content\", recieved %s", updatePostData.Content) diff --git a/Api/Routes.go b/Api/Routes.go index 2e45bca..c023cc4 100644 --- a/Api/Routes.go +++ b/Api/Routes.go @@ -23,6 +23,9 @@ func InitApiEndpoints() *mux.Router { router.HandleFunc("/post/{postID}", updatePost).Methods("PUT") router.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") + router.HandleFunc("/post/{postID}/image", createPostImage).Methods("POST") + router.HandleFunc("/post/{postID}/image/{imageID}", deletePostImage).Methods("DELETE") + //router.PathPrefix("/").Handler(http.StripPrefix("/images/", http.FileServer(http.Dir("./uploads")))) return router diff --git a/Api/test.jpg b/Api/test.jpg new file mode 100644 index 0000000..bba8bb9 Binary files /dev/null and b/Api/test.jpg differ diff --git a/Database/PostImages.go b/Database/PostImages.go new file mode 100644 index 0000000..70de44a --- /dev/null +++ b/Database/PostImages.go @@ -0,0 +1,32 @@ +package Database + +import ( + "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +func CreatePostImage(postImageData *Models.PostImage) error { + return DB.Session(&gorm.Session{FullSaveAssociations: true}). + Create(postImageData). + Error +} + +func GetPostImageById(id string) (Models.PostImage, error) { + var ( + postImageData Models.PostImage + err error + ) + + err = DB.Preload(clause.Associations). + First(&postImageData, "id = ?", id). + Error + + return postImageData, err +} + +func DeletePostImage(postImageData *Models.PostImage) error { + return DB.Session(&gorm.Session{FullSaveAssociations: true}). + Delete(postImageData). + Error +} diff --git a/Database/Posts.go b/Database/Posts.go index c0b31e7..3b84c58 100644 --- a/Database/Posts.go +++ b/Database/Posts.go @@ -70,12 +70,18 @@ func UpdatePost(id string, postData *Models.Post) (Models.Post, error) { err error ) + DB.Model(postData). + Where("id = ?", id). + Association("PostLinks"). + Replace(postData.PostLinks) + err = DB.Model(&Models.Post{}). Select("*"). Omit("id", "created_at", "updated_at", "deleted_at"). Where("id = ?", id). Updates(postData). Error + if err != nil { return Models.Post{}, err } diff --git a/Frontend/public/images/.gitkeep b/Frontend/public/images/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a30a150 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +FLAC_FILES = $(shell find . -type d -not -path '*/\.git/*' -not -path '*\/.git' -not -path '.') + +default: test build + +build: + go build -o SuddenImpactRecords main + +test: + for dir in ${FLAC_FILES}; do \ + go test $$dir; \ + done + diff --git a/Models/Posts.go b/Models/Posts.go index 5078189..a7c5e0d 100644 --- a/Models/Posts.go +++ b/Models/Posts.go @@ -29,25 +29,33 @@ const ( type PostLink struct { Base - PostID uuid.UUID `gorm:"type:uuid;column:post_foreign_key;not null;" json:"post_id"` + PostID uuid.UUID `gorm:"type:uuid;column:post_id;not null;" json:"post_id"` Link string `gorm:"not null" json:"link"` Type PostLinkType `gorm:"not null" json:"type"` } type PostImage struct { Base - PostID uuid.UUID `gorm:"type:uuid;column:post_foreign_key;not null;" json:"post_id"` + PostID uuid.UUID `gorm:"type:uuid;column:post_id;not null;" json:"post_id"` Filepath string `gorm:"not null" json:"filepath"` + Mimetype string `gorm:"not null" json:"mimetype"` + Size int64 `gorm:"not null"` } type PostVideo struct { Base - PostID uuid.UUID `gorm:"type:uuid;column:post_foreign_key;not null;" json:"post_id"` - Filepath string `gorm:"not null" json:"filepath"` + PostID uuid.UUID `gorm:"type:uuid;column:post_id;not null;" json:"post_id"` + Filepath string `json:"filepath"` + Mimetype string `json:"mimetype"` + Size int64 `json:"size"` + Url string `json:"url"` } type PostAudio struct { Base - PostID uuid.UUID `gorm:"type:uuid;column:post_foreign_key;not null;" json:"post_id"` - Filepath string `gorm:"not null" json:"filepath"` + PostID uuid.UUID `gorm:"type:uuid;column:post_id;not null;" json:"post_id"` + Filepath string `json:"filepath"` + Mimetype string `json:"mimetype"` + Size int64 `json:"size"` + Url string `json:"url"` } diff --git a/Util/Files.go b/Util/Files.go new file mode 100644 index 0000000..b0f208e --- /dev/null +++ b/Util/Files.go @@ -0,0 +1,67 @@ +package Util + +import ( + "errors" + "fmt" + "io/ioutil" + "os" + "strings" + + "github.com/gabriel-vasile/mimetype" +) + +type FileObject struct { + Filepath string + Mimetype string + Size int64 +} + +func WriteFile(fileBytes []byte, acceptedMime string) (FileObject, error) { + var ( + mime *mimetype.MIME + mimeSplit []string + file *os.File + fi os.FileInfo + fileObject FileObject + err error + ) + + mime = mimetype.Detect(fileBytes) + mimeSplit = strings.Split(mime.String(), "/") + + if mimeSplit[0] != acceptedMime { + return fileObject, errors.New("Invalid filetype provided") + } + + file, err = ioutil.TempFile( + fmt.Sprintf( + "./Frontend/public/%ss/", + mimeSplit[0], + ), + fmt.Sprintf( + "%ss-*%s", + mimeSplit[0], + mime.Extension(), + ), + ) + + if err != nil { + return fileObject, err + } + defer file.Close() + + _, err = file.Write(fileBytes) + + fi, err = file.Stat() + if err != nil { + return fileObject, err + } + + fileObject = FileObject{ + Filepath: file.Name(), + Mimetype: mime.String(), + Size: fi.Size(), + } + + return fileObject, err +} diff --git a/go.mod b/go.mod index 5a4c918..02ff28b 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/Kangaroux/go-map-schema v0.6.1 + github.com/gabriel-vasile/mimetype v1.4.0 github.com/gofrs/uuid v4.2.0+incompatible github.com/gorilla/mux v1.8.0 gorm.io/driver/postgres v1.3.1 @@ -22,5 +23,6 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.4 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect + golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 // indirect golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 55c7725..800de9d 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0FnbhiOsEro= +github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -144,6 +146,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 h1:Ugb8sMTWuWRC3+sz5WeN/4kejDx9BvIwnPUiJBjJE+8= +golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -156,6 +160,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/main.go b/main.go index 5687a24..fc8c9aa 100644 --- a/main.go +++ b/main.go @@ -20,5 +20,5 @@ func main() { // TODO: Run this within goroutine when running vue application // Start and listen to requests - http.ListenAndServe(":8080", router) + http.ListenAndServe(":8081", router) }