diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-25 13:23:37 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-25 13:23:37 -0700 |
commit | 099d000119447708d7d0d0482758d352438fa7e5 (patch) | |
tree | f4386ae2ff25a5b6b15c2e6442d4c56705e8271e /system/admin/upload/upload.go | |
parent | 7092fb8979869f3c09b364d454d8d8081bb7c0bc (diff) |
adding support for file upload type and API handler to fetch file info
Diffstat (limited to 'system/admin/upload/upload.go')
-rw-r--r-- | system/admin/upload/upload.go | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/system/admin/upload/upload.go b/system/admin/upload/upload.go index dbcdc17..35fb8f9 100644 --- a/system/admin/upload/upload.go +++ b/system/admin/upload/upload.go @@ -5,12 +5,16 @@ package upload import ( "fmt" "io" + "log" + "mime/multipart" "net/http" + "net/url" "os" "path/filepath" "strconv" "time" + "github.com/ponzu-cms/ponzu/system/db" "github.com/ponzu-cms/ponzu/system/item" ) @@ -86,16 +90,33 @@ func StoreFiles(req *http.Request) (map[string]string, error) { } // copy file from src to dst on disk - if _, err = io.Copy(dst, src); err != nil { + var size int64 + if size, err = io.Copy(dst, src); err != nil { err := fmt.Errorf("Failed to copy uploaded file to destination: %s", err) return nil, err } // add name:urlPath to req.PostForm to be inserted into db urlPath := fmt.Sprintf("/%s/%s/%d/%02d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename) - urlPaths[name] = urlPath + + // add upload information to db + go storeFileInfo(size, filename, urlPath, fds) } return urlPaths, nil } + +func storeFileInfo(size int64, filename, urlPath string, fds []*multipart.FileHeader) { + data := url.Values{ + "name": []string{filename}, + "path": []string{urlPath}, + "content_type": []string{fds[0].Header.Get("Content-Type")}, + "content_length": []string{fmt.Sprintf("%d", size)}, + } + + err := db.SetUpload(data) + if err != nil { + log.Println("Error saving file upload record to database:", err) + } +} |