summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-17 19:24:26 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-17 19:24:26 -0700
commit2a67b80c61a49b26c273a58d67c2cef9e7b549e7 (patch)
tree42c1b62637cbb4b18e86ee1d624c14e62ee338f5
parent925241488724c91394b54955c90eb78302382732 (diff)
updating handler code to support new timestamp and updated fields
-rw-r--r--management/editor/editor.go24
-rw-r--r--system/admin/handlers.go15
-rw-r--r--system/admin/upload.go54
3 files changed, 33 insertions, 60 deletions
diff --git a/management/editor/editor.go b/management/editor/editor.go
index bf95f6e..eff3274 100644
--- a/management/editor/editor.go
+++ b/management/editor/editor.go
@@ -49,18 +49,18 @@ func Form(post Editable, fields ...Field) ([]byte, error) {
<div class="input-field col s6">
<label class="active">MM</label>
<select class="month __ponzu browser-default">
- <option value="0">Jan - 01</option>
- <option value="1">Feb - 02</option>
- <option value="2">Mar - 03</option>
- <option value="3">Apr - 04</option>
- <option value="4">May - 05</option>
- <option value="5">Jun - 06</option>
- <option value="6">Jul - 07</option>
- <option value="7">Aug - 08</option>
- <option value="8">Sep - 09</option>
- <option value="9">Oct - 10</option>
- <option value="10">Nov - 11</option>
- <option value="11">Dec - 12</option>
+ <option value="1">Jan - 01</option>
+ <option value="2">Feb - 02</option>
+ <option value="3">Mar - 03</option>
+ <option value="4">Apr - 04</option>
+ <option value="5">May - 05</option>
+ <option value="6">Jun - 06</option>
+ <option value="7">Jul - 07</option>
+ <option value="8">Aug - 08</option>
+ <option value="9">Sep - 09</option>
+ <option value="10">Oct - 10</option>
+ <option value="11">Nov - 11</option>
+ <option value="12">Dec - 12</option>
</select>
</div>
<div class="input-field col s2">
diff --git a/system/admin/handlers.go b/system/admin/handlers.go
index d492335..edf351d 100644
--- a/system/admin/handlers.go
+++ b/system/admin/handlers.go
@@ -435,21 +435,18 @@ func editHandler(res http.ResponseWriter, req *http.Request) {
cid := req.FormValue("id")
t := req.FormValue("type")
ts := req.FormValue("timestamp")
+ up := req.FormValue("updated")
// create a timestamp if one was not set
- date := make(map[string]int)
if ts == "" {
- now := time.Now()
- date["year"] = now.Year()
- date["month"] = int(now.Month())
- date["day"] = now.Day()
-
- // create timestamp format 'yyyy-mm-dd' and set in PostForm for
- // db insertion
- ts = fmt.Sprintf("%d-%02d-%02d", date["year"], date["month"], date["day"])
+ ts := fmt.Sprintf("%d", time.Now().Unix()*1000)
req.PostForm.Set("timestamp", ts)
}
+ if up == "" {
+ req.PostForm.Set("updated", ts)
+ }
+
urlPaths, err := storeFileUploads(req)
if err != nil {
fmt.Println(err)
diff --git a/system/admin/upload.go b/system/admin/upload.go
index a2cde4c..7f2a4fa 100644
--- a/system/admin/upload.go
+++ b/system/admin/upload.go
@@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"strconv"
- "strings"
"time"
)
@@ -17,45 +16,16 @@ func storeFileUploads(req *http.Request) (map[string]string, error) {
return nil, fmt.Errorf("%s", err)
}
- ts := req.FormValue("timestamp")
+ ts := req.FormValue("timestamp") // timestamp in milliseconds since unix epoch
- // To use for FormValue name:urlPath
- urlPaths := make(map[string]string)
-
- // get ts values individually to use as directory names when storing
- // uploaded images
- date := make(map[string]int)
if ts == "" {
- now := time.Now()
- date["year"] = now.Year()
- date["month"] = int(now.Month())
- date["day"] = now.Day()
-
- // create timestamp format 'yyyy-mm-dd' and set in PostForm for
- // db insertion
- ts = fmt.Sprintf("%d-%02d-%02d", date["year"], date["month"], date["day"])
- req.PostForm.Set("timestamp", ts)
- } else {
- tsParts := strings.Split(ts, "-")
- year, err := strconv.Atoi(tsParts[0])
- if err != nil {
- return nil, fmt.Errorf("%s", err)
- }
+ ts = fmt.Sprintf("%d", time.Now().Unix()*1000) // Unix() returns seconds since unix epoch
+ }
- month, err := strconv.Atoi(tsParts[1])
- if err != nil {
- return nil, fmt.Errorf("%s", err)
- }
+ req.Form.Set("timestamp", ts)
- day, err := strconv.Atoi(tsParts[2])
- if err != nil {
- return nil, fmt.Errorf("%s", err)
- }
-
- date["year"] = year
- date["month"] = month
- date["day"] = day
- }
+ // To use for FormValue name:urlPath
+ urlPaths := make(map[string]string)
// get or create upload directory to save files from request
pwd, err := os.Getwd()
@@ -64,11 +34,17 @@ func storeFileUploads(req *http.Request) (map[string]string, error) {
return nil, err
}
- tsParts := strings.Split(ts, "-")
+ i, err := strconv.ParseInt(ts, 10, 64)
+ if err != nil {
+ return nil, err
+ }
+
+ tm := time.Unix(int64(i/1000), int64(i%1000))
+
urlPathPrefix := "api"
uploadDirName := "uploads"
- uploadDir := filepath.Join(pwd, uploadDirName, tsParts[0], tsParts[1])
+ uploadDir := filepath.Join(pwd, uploadDirName, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%d", tm.Month()))
err = os.MkdirAll(uploadDir, os.ModeDir|os.ModePerm)
// loop over all files and save them to disk
@@ -104,7 +80,7 @@ func storeFileUploads(req *http.Request) (map[string]string, error) {
}
// add name:urlPath to req.PostForm to be inserted into db
- urlPath := fmt.Sprintf("/%s/%s/%s/%s/%s", urlPathPrefix, uploadDirName, tsParts[0], tsParts[1], filename)
+ urlPath := fmt.Sprintf("/%s/%s/%d/%d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename)
urlPaths[name] = urlPath
}