上传文件功能

This commit is contained in:
deepzz0
2016-12-18 00:16:06 +08:00
parent 3e50cfa248
commit 0b1e034346
8 changed files with 128 additions and 66 deletions

37
api.go
View File

@@ -121,7 +121,7 @@ func apiPassword(c *gin.Context) {
return
}
Ei.Password = newPwd
responseNotice(c, NOTICE_SUCCESS, "更成功", "")
responseNotice(c, NOTICE_SUCCESS, "更成功", "")
}
func apiPostDelete(c *gin.Context) {
@@ -294,7 +294,7 @@ func apiSerieAdd(c *gin.Context) {
if err == nil && mid > 0 {
serie := QuerySerie(int32(mid))
if serie == nil {
responseNotice(c, NOTICE_NOTICE, "not found serie", "")
responseNotice(c, NOTICE_NOTICE, "专题不存在", "")
return
}
serie.Name = name
@@ -398,19 +398,26 @@ func apiTrashRecover(c *gin.Context) {
}
func apiFileUpload(c *gin.Context) {
// file, header , err := c.Request.FormFile("upload")
// filename := header.Filename
// fmt.Println(header.Filename)
// out, err := os.Create("./tmp/"+filename+".png")
// if err != nil {
// log.Fatal(err)
// }
// defer out.Close()
// _, err = io.Copy(out, file)
// if err != nil {
// log.Fatal(err)
// }
type Size interface {
Size() int64
}
file, header, err := c.Request.FormFile("upload")
if err != nil {
responseNotice(c, NOTICE_NOTICE, "上传失败", "")
return
}
s, ok := file.(Size)
if !ok {
responseNotice(c, NOTICE_NOTICE, "文件太大", "")
return
}
filename := strings.ToLower(header.Filename)
url, err := Upload(filename, s.Size(), file)
if err != nil {
responseNotice(c, NOTICE_NOTICE, "上传失败", "")
return
}
responseNotice(c, NOTICE_SUCCESS, url, "")
}
func responseNotice(c *gin.Context, typ, content, hl string) {

View File

@@ -1,5 +1,5 @@
# 运行模式 dev or prod
runmode: prod
runmode: dev
# 回收箱保留48小时
trash: -48
# 定时清理回收箱,%d小时
@@ -37,8 +37,8 @@ staticversion: 2
kodo:
name: eiblog
domain: st.deepzz.com
accesskey:
secretkey:
accesskey: MB6AXl_Sj_mmFsL-Lt59Dml2Vmy2o8XMmiCbbSeC
secretkey: BIrMy0fsZ0_SHNceNXk3eDuo7WmVYzj2-zrmd5Tf
# 运行模式
mode:
# you can fix certfile, keyfile, domain

20
glide.lock generated
View File

@@ -1,28 +1,28 @@
hash: 4b70e76a2e830e97033c06d0e5a90c3199985ff5070bdf8364b1feca63d5caa5
updated: 2016-11-16T21:02:51.442531751+08:00
updated: 2016-12-17T23:59:11.607037369+08:00
imports:
- name: github.com/boj/redistore
version: fc113767cd6b051980f260d6dbe84b2740c46ab0
- name: github.com/eiblog/blackfriday
version: c0ec111761ae784fe31cc076f2fa0e2d2216d623
- name: github.com/eiblog/utils
version: 06c7dc24b885333250d26e34d2932d4a7cfa8b46
version: ad2f63940c4f16d0dbfc3f4df59e8cb7af0f80ec
subpackages:
- logd
- mgo
- tmpl
- uuid
- name: github.com/garyburd/redigo
version: 80f7de34463b0ed3d7c61303e5619efe1b227f92
version: f8c71fc158ba13d50a7f5d8f10ea18ec49463c73
subpackages:
- internal
- redis
- name: github.com/gin-gonic/contrib
version: cf28f84316e50a05a59d174b37c26ad9a43a1201
version: 45b1fbf9c69a8b8905d20f665b559d03f932db84
subpackages:
- sessions
- name: github.com/gin-gonic/gin
version: bb159f9260a262fccbdbd6f37f8575d1e7e6aaa0
version: e2212d40c62a98b388a5eb48ecbdcf88534688ba
subpackages:
- binding
- render
@@ -35,15 +35,21 @@ imports:
- name: github.com/gorilla/securecookie
version: fa5329f913702981df43dcb2a380bac429c810b5
- name: github.com/gorilla/sessions
version: ca9ada44574153444b00d3fd9c8559e4cc95f896
version: 52389059572b4736b2b7d899b0242ecc449c8779
- name: github.com/manucorporat/sse
version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d
- name: github.com/mattn/go-isatty
version: 30a891c33c7cde7b02a981314b4228ec99380cca
- name: github.com/shurcooL/sanitized_anchor_name
version: 1dba4b3954bc059efc3991ec364f9f9a35f597d2
- name: golang.org/x/net
version: f315505cf3349909cdf013ea56690da34e96a451
subpackages:
- context
- name: golang.org/x/sys
version: d75a52659825e75fff6158388dddc6a5b04f9ba5
subpackages:
- unix
- name: gopkg.in/go-playground/validator.v8
version: c193cecd124b5cc722d7ee5538e945bdb3348435
- name: gopkg.in/mgo.v2
@@ -56,7 +62,7 @@ imports:
- name: gopkg.in/yaml.v2
version: a5b47d31c556af34a302ce5d659e6fea44d90de0
- name: qiniupkg.com/api.v7
version: ad7023b87b2f5c169812da49fe44900be1c4e5c2
version: 7cfd4b639917bf924d8c1cd17a6d61175e809066
subpackages:
- api
- auth/qbox

View File

@@ -1,13 +1,12 @@
package main
import (
"crypto/md5"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"io"
"path/filepath"
"github.com/eiblog/utils/logd"
"github.com/eiblog/eiblog/setting"
"qiniupkg.com/api.v7/conf"
"qiniupkg.com/api.v7/kodo"
"qiniupkg.com/api.v7/kodocli"
@@ -20,12 +19,6 @@ type bucket struct {
secretKey string
}
var buckets = map[string]*bucket{}
func getBucket(typ string) *bucket {
return buckets[typ]
}
type PutRet struct {
Hash string `json:"hash"`
Key string `json:"key"`
@@ -40,31 +33,19 @@ func onProgress(fsize, uploaded int64) {
}
}
func upload(typ string, filepath string) {
bucket := getBucket(typ)
if bucket == nil {
logd.Debug("invalid type:", typ)
return
func Upload(name string, size int64, data io.Reader) (string, error) {
if setting.Conf.Kodo.AccessKey == "" || setting.Conf.Kodo.SecretKey == "" {
return "", errors.New("qiniu config error")
}
file, err := os.Open(filepath)
if err != nil {
logd.Debugf("%s\n", 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
conf.ACCESS_KEY = setting.Conf.Kodo.AccessKey
conf.SECRET_KEY = setting.Conf.Kodo.SecretKey
// 创建一个client
c := kodo.New(0, nil)
// 设置上传的策略
policy := &kodo.PutPolicy{
Scope: bucket.name,
Scope: setting.Conf.Kodo.Name,
Expires: 3600,
InsertOnly: 1,
}
@@ -75,18 +56,30 @@ func upload(typ string, filepath string) {
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\n", res)
return
ext := filepath.Ext(name)
var key string
switch ext {
case ".bmp", ".png", ".jpg", ".gif", ".ico":
key = "blog/img/" + name
case ".mov", ".mp4":
key = "blog/video/" + name
case ".go", ".js", ".css", ".cpp", ".php", ".rb", ".java", ".py", ".sql", ".lua", ".html", ".sh", ".xml", ".cs":
key = "blog/code/" + name
case ".txt", ".md", ".ini", ".yaml", ".yml", ".doc", ".ppt", ".pdf":
key = "blog/document/" + name
case ".zip", ".rar", ".tar", ".gz":
key = "blog/archive/" + name
default:
return "", errors.New("不支持的文件类型")
}
url := kodo.MakeBaseUrl(bucket.domain, key)
fmt.Printf("url: %s\n", url)
var ret PutRet
var extra = kodocli.PutExtra{OnProgress: onProgress}
err := uploader.Put(nil, &ret, token, key, data, size, &extra)
if err != nil {
return "", err
}
url := kodo.MakeBaseUrl(setting.Conf.Kodo.Domain, ret.Key)
return url, nil
}

21
qiniu_test.go Normal file
View File

@@ -0,0 +1,21 @@
// Package main provides ...
package main
import (
"os"
"testing"
)
func TestUpload(t *testing.T) {
path := "/Users/chen/Desktop/png-MicroService-by-StuQ.png"
file, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
info, _ := file.Stat()
url, err := Upload(info.Name(), info.Size(), file)
if err != nil {
t.Fatal(err)
}
t.Log(url)
}

13
static/feed.xml Executable file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Deepzz's Blog</title>
<link>https://deepzz.com</link>
<description>不抛弃,不放弃</description>
<atom:link href="https://deepzz.com/rss.html" rel="self"/>
<atom:link href="https://deepzz.superfeedr.com/" rel="hub"/>
<language>zh-CN</language>
<lastBuildDate>Sun, 18 Dec 2016 00:15:03 +0800</lastBuildDate>
</channel>
</rss>

6
static/opensearch.xml Executable file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Deepzz's Blog</ShortName>
<Description>不抛弃,不放弃</Description>
<Url type="text/html" template="https://deepzz.com/search.html?q={searchTerms}" />
</OpenSearchDescription>

16
static/sitemap.xml Executable file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://deepzz.com/post/blogroll.html</loc>
<lastmod>2016-12-17</lastmod>
<priority>0.6</priority>
</url>
<url>
<loc>https://deepzz.com/post/about.html</loc>
<lastmod>2016-12-17</lastmod>
<priority>0.6</priority>
</url>
</urlset>