storage/app: add additional metadata keys

This also moves the metadata generation into processUpload; it didn't
really benefit from being in a separate function.

Change-Id: Ia9fb3455cf8851ea89171bc7e135d69e6734eafc
Reviewed-on: https://go-review.googlesource.com/35210
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/storage/app/upload.go b/storage/app/upload.go
index adb2577..c24d6ae 100644
--- a/storage/app/upload.go
+++ b/storage/app/upload.go
@@ -13,6 +13,8 @@
 	"net/http"
 	"net/url"
 	"sort"
+	"strings"
+	"time"
 
 	"golang.org/x/net/context"
 	"golang.org/x/perf/storage/benchfmt"
@@ -82,6 +84,8 @@
 	var upload *db.Upload
 	var fileids []string
 
+	uploadtime := time.Now().UTC().Format(time.RFC3339)
+
 	for i := 0; ; i++ {
 		p, err := mr.NextPart()
 		if err == io.EOF {
@@ -111,7 +115,21 @@
 		// is invalid (contains no valid records) it needs to
 		// be rejected and the Cloud Storage upload aborted.
 
-		meta := fileMetadata(user, upload.ID, i)
+		meta := map[string]string{
+			"uploadid":   upload.ID,
+			"fileid":     fmt.Sprintf("%s/%d", upload.ID, filenum),
+			"uploadtime": uploadtime,
+		}
+		name := p.FileName()
+		if slash := strings.LastIndexAny(name, `/\`); slash >= 0 {
+			name = name[slash+1:]
+		}
+		if name != "" {
+			m["uploadfile"] = name
+		}
+		if user != "" {
+			m["by"] = user
+		}
 
 		// We need to do two things with the incoming data:
 		// - Write it to permanent storage via a.FS
@@ -165,6 +183,8 @@
 			return err
 		}
 	}
+	// Write a blank line to separate metadata from user-generated content.
+	fmt.Fprintf(fw, "\n")
 
 	// TODO(quentin): Add a separate goroutine and buffer for writes to fw?
 	tr := io.TeeReader(p, fw)
@@ -188,18 +208,3 @@
 		}
 	}
 }
-
-// fileMetadata returns the extra metadata fields associated with an
-// uploaded file.
-func fileMetadata(user string, uploadid string, filenum int) map[string]string {
-	// TODO(quentin): Add the upload time.
-	// TODO(quentin): Add other fields?
-	m := map[string]string{
-		"uploadid": uploadid,
-		"fileid":   fmt.Sprintf("%s/%d", uploadid, filenum),
-	}
-	if user != "" {
-		m["by"] = user
-	}
-	return m
-}