From 98cde57aa3e3726fa3239d9c3452f5fa2f1e78c1 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Tue, 1 Nov 2016 22:27:13 +0800 Subject: [PATCH] add kodo --- back.go | 2 +- conf/app.yml | 14 +++--- disqus_test.go | 2 +- elasticsearch_test.go | 7 --- front.go | 2 +- qiniu.go | 97 ++++++++++++++++++++++++++++++++++++++++ setting/setting.go | 6 +++ views/admin/post.html | 9 ++++ views/admin/profile.html | 4 +- views/homeLayout.html | 2 +- views/st_blog_css.css | 2 +- 11 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 qiniu.go diff --git a/back.go b/back.go index 1112d92..48679a2 100644 --- a/back.go +++ b/back.go @@ -75,7 +75,7 @@ func HandleLoginPost(c *gin.Context) { } func GetBack() gin.H { - return gin.H{"Author": Ei.Username} + return gin.H{"Author": Ei.Username, "Kodo": setting.Conf.Kodo} } // 个人配置 diff --git a/conf/app.yml b/conf/app.yml index 29f4335..dcfbb90 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -1,7 +1,5 @@ # 运行模式 dev or prod runmode: prod -# 静态文件版本 -staticversion: 2 # 回收箱保留48小时 trash: -48 # 定时清理回收箱,%d小时 @@ -14,12 +12,8 @@ pagesize: 20 length: 400 # 截取预览标识 identifier: -# favicon -favicon: //st.deepzz.com/static/img/favicon.ico # 起始ID,预留id不时之需, 不用管 startid: 11 -# cdn: //domain.com -static: //st.deepzz.com # elasticsearch url searchurl: http://elasticsearch:9200 # superfeedr shortname @@ -39,6 +33,14 @@ google: tid: UA-77251712-1 v: "1" t: pageview +# 静态文件版本 +staticversion: 2 +# 七牛CDN +kodo: + name: eiblog + domain: st.deepzz.com + accesskey: + secretkey: # 运行模式 mode: # you can fix certfile, keyfile, domain diff --git a/disqus_test.go b/disqus_test.go index e694948..f5d9a3f 100644 --- a/disqus_test.go +++ b/disqus_test.go @@ -5,5 +5,5 @@ import ( ) func TestDisqus(t *testing.T) { - CommentsCount() + PostsCount() } diff --git a/elasticsearch_test.go b/elasticsearch_test.go index 5461253..43b4bd6 100644 --- a/elasticsearch_test.go +++ b/elasticsearch_test.go @@ -61,13 +61,6 @@ func TestIndexDocument(t *testing.T) { } } -func TestIndexQuerySimple(t *testing.T) { - _, err := IndexQuerySimple(INDEX, TYPE, 10, 1, "JS") - if err != nil { - t.Error(err) - } -} - func TestIndexQueryDSL(t *testing.T) { kw := "实现访问限制" dsl := map[string]interface{}{ diff --git a/front.go b/front.go index e05efc6..e795915 100644 --- a/front.go +++ b/front.go @@ -63,7 +63,7 @@ func GetBase() gin.H { "BTitle": Ei.BTitle, "BeiAn": Ei.BeiAn, "Domain": setting.Conf.Mode.Domain, - "Static": setting.Conf.Static, + "Kodo": setting.Conf.Kodo, } } diff --git a/qiniu.go b/qiniu.go new file mode 100644 index 0000000..3615c87 --- /dev/null +++ b/qiniu.go @@ -0,0 +1,97 @@ +package main + +import ( + "crypto/md5" + "fmt" + "io/ioutil" + "os" + "path" + + "github.com/eiblog/utils/logd" + "qiniupkg.com/api.v7/conf" + "qiniupkg.com/api.v7/kodo" + "qiniupkg.com/api.v7/kodocli" +) + +type bucket struct { + name string + domain string + accessKey string + secretKey string +} + +var buckets = map[string]*bucket{ + "avatar": &bucket{name: "yooohome-avatar", domain: "o9gich2l7.bkt.clouddn.com", + accessKey: "porNOigaMUjIcO9I5ePPGJmO1fchhqnlSI0oBrpY", secretKey: "l0EawgePDmXzMYBvC_7WALUvD69iDzMgay7rX77b"}, + "topic": &bucket{name: "yooohome-topic", domain: "o9gie1fqp.bkt.clouddn.com", + accessKey: "porNOigaMUjIcO9I5ePPGJmO1fchhqnlSI0oBrpY", secretKey: "l0EawgePDmXzMYBvC_7WALUvD69iDzMgay7rX77b"}, +} + +func getBucket(typ string) *bucket { + return buckets[typ] +} + +type PutRet struct { + Hash string `json:"hash"` + Key string `json:"key"` +} + +func onProgress(fsize, uploaded int64) { + d := int(float64(uploaded) / float64(fsize) * 100) + if fsize == uploaded { + fmt.Printf("\rUpload completed! ") + } else { + fmt.Printf("\r%02d%% uploaded ", int(d)) + } +} + +func upload(typ string, filepath string) { + bucket := getBucket(typ) + if bucket == nil { + logd.Debug("invalid type:", typ) + return + } + + file, err := os.Open(filepath) + if err != nil { + logd.Debugf("%s", err.Error()) + return + } + data, err := ioutil.ReadAll(file) + file.Close() + chksum := fmt.Sprintf("%x", md5.Sum(data)) + ext := path.Ext(filepath) + + conf.ACCESS_KEY = bucket.accessKey + conf.SECRET_KEY = bucket.secretKey + // 创建一个client + c := kodo.New(0, nil) + + // 设置上传的策略 + policy := &kodo.PutPolicy{ + Scope: bucket.name, + Expires: 3600, + InsertOnly: 1, + } + + // 生成一个上传token + token := c.MakeUptoken(policy) + // 构建一个uploader + zone := 0 + uploader := kodocli.NewUploader(zone, nil) + + var ret PutRet + key := fmt.Sprintf("%s-%s%s", typ, chksum, ext) + + fmt.Printf("Uploading .....") + var extra = kodocli.PutExtra{OnProgress: onProgress} + res := uploader.PutFile(nil, &ret, token, key, filepath, &extra) + // 打印返回的信息 + if res != nil { + logd.Debugf("failed to upload patch file: %v", res) + return + } + + url := kodo.MakeBaseUrl(bucket.domain, key) + fmt.Printf("url: %s\n", url) +} diff --git a/setting/setting.go b/setting/setting.go index f607db8..96507f9 100644 --- a/setting/setting.go +++ b/setting/setting.go @@ -48,6 +48,12 @@ type Config struct { V string T string } + Kodo struct { // 七牛CDN + Name string + Domain string + AccessKey string + SecretKey string + } Mode RunMode // 运行模式 Twitter string // twitter地址 Blogger struct { // 初始化数据 diff --git a/views/admin/post.html b/views/admin/post.html index 810fc25..625b5b7 100644 --- a/views/admin/post.html +++ b/views/admin/post.html @@ -55,6 +55,15 @@

+
+ + +