This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
photos.go
executable file
·65 lines (59 loc) · 1.45 KB
/
photos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package mem
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
)
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie, req *http.Request) *http.Cookie {
defer src.Close()
fName := getSha(src) + ".jpg"
wd, _ := os.Getwd()
path := filepath.Join(wd, "assets", "imgs", fName)
dst, _ := os.Create(path)
defer dst.Close()
src.Seek(0, 0)
io.Copy(dst, src)
return addPhoto(fName, c, req)
}
func addPhoto(fName string, c *http.Cookie, req *http.Request) *http.Cookie {
xs := strings.Split(c.Value, "|")
// memcache
id := xs[0]
m2 := retrieveMemc(req, id)
m2.Pictures = append(m2.Pictures, fName)
mm := marshalModel(m2)
b64 := base64.URLEncoding.EncodeToString(mm)
storeMemc([]byte(b64), id, req)
// cookie
usrData := xs[1]
bs, err := base64.URLEncoding.DecodeString(usrData)
if err != nil {
ctx := appengine.NewContext(req)
log.Errorf(ctx, "Error decoding base64: %s", err)
}
m := unmarshalModel(bs)
m.Pictures = append(m.Pictures, fName)
mm = marshalModel(m)
b64 = base64.URLEncoding.EncodeToString(mm)
code := getCode(b64) // hmac
cookie := &http.Cookie{
Name: "session-id",
Value: id + "|" + b64 + "|" + code,
// Secure: true,
HttpOnly: true,
}
return cookie
}
func getSha(src multipart.File) string {
h := sha1.New()
io.Copy(h, src)
return fmt.Sprintf("%x", h.Sum(nil))
}