Ma Suhyeon

Implement extraction upload

......@@ -34,4 +34,5 @@
# End of https://www.toptal.com/developers/gitignore/api/go,vscode
__debug_bin
config.json
\ No newline at end of file
config.json
data
\ No newline at end of file
......
......@@ -32,6 +32,7 @@ func NewApp(config Config) *App {
app.router = mux.NewRouter()
app.router.HandleFunc("/users", app.PostUsers).Methods("POST")
app.router.HandleFunc("/users/tokens", app.PostTokens).Methods("POST")
app.router.Handle("/extractions", app.WithAuth(app.PostExtractions)).Methods("Post")
return app
}
......
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/uuid"
)
func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
userNo := r.Context().Value(PropUserNo).(uint64)
r.ParseMultipartForm(32 << 20)
form, _, err := r.FormFile("file")
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
defer form.Close()
dir := fmt.Sprintf("data/%d", userNo)
os.MkdirAll(dir, 0644)
name := strings.Replace(uuid.New().String(), "-", "", -1)
file, err := os.Create(fmt.Sprintf("%s/%s", dir, name))
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
defer file.Close()
_, err = io.Copy(file, form)
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
w.Write([]byte("success"))
}
......@@ -5,6 +5,7 @@ go 1.15
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/google/uuid v1.1.2
github.com/gorilla/mux v1.8.0
github.com/jmoiron/sqlx v1.2.0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
......
......@@ -4,6 +4,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
......