mirror of
https://github.com/eiblog/eiblog.git
synced 2026-03-01 00:34:58 +08:00
上传文件功能
This commit is contained in:
37
api.go
37
api.go
@@ -121,7 +121,7 @@ func apiPassword(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
Ei.Password = newPwd
|
Ei.Password = newPwd
|
||||||
responseNotice(c, NOTICE_SUCCESS, "更改成功", "")
|
responseNotice(c, NOTICE_SUCCESS, "更新成功", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiPostDelete(c *gin.Context) {
|
func apiPostDelete(c *gin.Context) {
|
||||||
@@ -294,7 +294,7 @@ func apiSerieAdd(c *gin.Context) {
|
|||||||
if err == nil && mid > 0 {
|
if err == nil && mid > 0 {
|
||||||
serie := QuerySerie(int32(mid))
|
serie := QuerySerie(int32(mid))
|
||||||
if serie == nil {
|
if serie == nil {
|
||||||
responseNotice(c, NOTICE_NOTICE, "not found serie", "")
|
responseNotice(c, NOTICE_NOTICE, "专题不存在", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
serie.Name = name
|
serie.Name = name
|
||||||
@@ -398,19 +398,26 @@ func apiTrashRecover(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func apiFileUpload(c *gin.Context) {
|
func apiFileUpload(c *gin.Context) {
|
||||||
|
type Size interface {
|
||||||
// file, header , err := c.Request.FormFile("upload")
|
Size() int64
|
||||||
// filename := header.Filename
|
}
|
||||||
// fmt.Println(header.Filename)
|
file, header, err := c.Request.FormFile("upload")
|
||||||
// out, err := os.Create("./tmp/"+filename+".png")
|
if err != nil {
|
||||||
// if err != nil {
|
responseNotice(c, NOTICE_NOTICE, "上传失败", "")
|
||||||
// log.Fatal(err)
|
return
|
||||||
// }
|
}
|
||||||
// defer out.Close()
|
s, ok := file.(Size)
|
||||||
// _, err = io.Copy(out, file)
|
if !ok {
|
||||||
// if err != nil {
|
responseNotice(c, NOTICE_NOTICE, "文件太大", "")
|
||||||
// log.Fatal(err)
|
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) {
|
func responseNotice(c *gin.Context, typ, content, hl string) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# 运行模式 dev or prod
|
# 运行模式 dev or prod
|
||||||
runmode: prod
|
runmode: dev
|
||||||
# 回收箱保留48小时
|
# 回收箱保留48小时
|
||||||
trash: -48
|
trash: -48
|
||||||
# 定时清理回收箱,%d小时
|
# 定时清理回收箱,%d小时
|
||||||
@@ -37,8 +37,8 @@ staticversion: 2
|
|||||||
kodo:
|
kodo:
|
||||||
name: eiblog
|
name: eiblog
|
||||||
domain: st.deepzz.com
|
domain: st.deepzz.com
|
||||||
accesskey:
|
accesskey: MB6AXl_Sj_mmFsL-Lt59Dml2Vmy2o8XMmiCbbSeC
|
||||||
secretkey:
|
secretkey: BIrMy0fsZ0_SHNceNXk3eDuo7WmVYzj2-zrmd5Tf
|
||||||
# 运行模式
|
# 运行模式
|
||||||
mode:
|
mode:
|
||||||
# you can fix certfile, keyfile, domain
|
# you can fix certfile, keyfile, domain
|
||||||
|
|||||||
20
glide.lock
generated
20
glide.lock
generated
@@ -1,28 +1,28 @@
|
|||||||
hash: 4b70e76a2e830e97033c06d0e5a90c3199985ff5070bdf8364b1feca63d5caa5
|
hash: 4b70e76a2e830e97033c06d0e5a90c3199985ff5070bdf8364b1feca63d5caa5
|
||||||
updated: 2016-11-16T21:02:51.442531751+08:00
|
updated: 2016-12-17T23:59:11.607037369+08:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/boj/redistore
|
- name: github.com/boj/redistore
|
||||||
version: fc113767cd6b051980f260d6dbe84b2740c46ab0
|
version: fc113767cd6b051980f260d6dbe84b2740c46ab0
|
||||||
- name: github.com/eiblog/blackfriday
|
- name: github.com/eiblog/blackfriday
|
||||||
version: c0ec111761ae784fe31cc076f2fa0e2d2216d623
|
version: c0ec111761ae784fe31cc076f2fa0e2d2216d623
|
||||||
- name: github.com/eiblog/utils
|
- name: github.com/eiblog/utils
|
||||||
version: 06c7dc24b885333250d26e34d2932d4a7cfa8b46
|
version: ad2f63940c4f16d0dbfc3f4df59e8cb7af0f80ec
|
||||||
subpackages:
|
subpackages:
|
||||||
- logd
|
- logd
|
||||||
- mgo
|
- mgo
|
||||||
- tmpl
|
- tmpl
|
||||||
- uuid
|
- uuid
|
||||||
- name: github.com/garyburd/redigo
|
- name: github.com/garyburd/redigo
|
||||||
version: 80f7de34463b0ed3d7c61303e5619efe1b227f92
|
version: f8c71fc158ba13d50a7f5d8f10ea18ec49463c73
|
||||||
subpackages:
|
subpackages:
|
||||||
- internal
|
- internal
|
||||||
- redis
|
- redis
|
||||||
- name: github.com/gin-gonic/contrib
|
- name: github.com/gin-gonic/contrib
|
||||||
version: cf28f84316e50a05a59d174b37c26ad9a43a1201
|
version: 45b1fbf9c69a8b8905d20f665b559d03f932db84
|
||||||
subpackages:
|
subpackages:
|
||||||
- sessions
|
- sessions
|
||||||
- name: github.com/gin-gonic/gin
|
- name: github.com/gin-gonic/gin
|
||||||
version: bb159f9260a262fccbdbd6f37f8575d1e7e6aaa0
|
version: e2212d40c62a98b388a5eb48ecbdcf88534688ba
|
||||||
subpackages:
|
subpackages:
|
||||||
- binding
|
- binding
|
||||||
- render
|
- render
|
||||||
@@ -35,15 +35,21 @@ imports:
|
|||||||
- name: github.com/gorilla/securecookie
|
- name: github.com/gorilla/securecookie
|
||||||
version: fa5329f913702981df43dcb2a380bac429c810b5
|
version: fa5329f913702981df43dcb2a380bac429c810b5
|
||||||
- name: github.com/gorilla/sessions
|
- name: github.com/gorilla/sessions
|
||||||
version: ca9ada44574153444b00d3fd9c8559e4cc95f896
|
version: 52389059572b4736b2b7d899b0242ecc449c8779
|
||||||
- name: github.com/manucorporat/sse
|
- name: github.com/manucorporat/sse
|
||||||
version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d
|
version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d
|
||||||
|
- name: github.com/mattn/go-isatty
|
||||||
|
version: 30a891c33c7cde7b02a981314b4228ec99380cca
|
||||||
- name: github.com/shurcooL/sanitized_anchor_name
|
- name: github.com/shurcooL/sanitized_anchor_name
|
||||||
version: 1dba4b3954bc059efc3991ec364f9f9a35f597d2
|
version: 1dba4b3954bc059efc3991ec364f9f9a35f597d2
|
||||||
- name: golang.org/x/net
|
- name: golang.org/x/net
|
||||||
version: f315505cf3349909cdf013ea56690da34e96a451
|
version: f315505cf3349909cdf013ea56690da34e96a451
|
||||||
subpackages:
|
subpackages:
|
||||||
- context
|
- context
|
||||||
|
- name: golang.org/x/sys
|
||||||
|
version: d75a52659825e75fff6158388dddc6a5b04f9ba5
|
||||||
|
subpackages:
|
||||||
|
- unix
|
||||||
- name: gopkg.in/go-playground/validator.v8
|
- name: gopkg.in/go-playground/validator.v8
|
||||||
version: c193cecd124b5cc722d7ee5538e945bdb3348435
|
version: c193cecd124b5cc722d7ee5538e945bdb3348435
|
||||||
- name: gopkg.in/mgo.v2
|
- name: gopkg.in/mgo.v2
|
||||||
@@ -56,7 +62,7 @@ imports:
|
|||||||
- name: gopkg.in/yaml.v2
|
- name: gopkg.in/yaml.v2
|
||||||
version: a5b47d31c556af34a302ce5d659e6fea44d90de0
|
version: a5b47d31c556af34a302ce5d659e6fea44d90de0
|
||||||
- name: qiniupkg.com/api.v7
|
- name: qiniupkg.com/api.v7
|
||||||
version: ad7023b87b2f5c169812da49fe44900be1c4e5c2
|
version: 7cfd4b639917bf924d8c1cd17a6d61175e809066
|
||||||
subpackages:
|
subpackages:
|
||||||
- api
|
- api
|
||||||
- auth/qbox
|
- auth/qbox
|
||||||
|
|||||||
75
qiniu.go
75
qiniu.go
@@ -1,13 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"os"
|
"path/filepath"
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/eiblog/utils/logd"
|
"github.com/eiblog/eiblog/setting"
|
||||||
"qiniupkg.com/api.v7/conf"
|
"qiniupkg.com/api.v7/conf"
|
||||||
"qiniupkg.com/api.v7/kodo"
|
"qiniupkg.com/api.v7/kodo"
|
||||||
"qiniupkg.com/api.v7/kodocli"
|
"qiniupkg.com/api.v7/kodocli"
|
||||||
@@ -20,12 +19,6 @@ type bucket struct {
|
|||||||
secretKey string
|
secretKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
var buckets = map[string]*bucket{}
|
|
||||||
|
|
||||||
func getBucket(typ string) *bucket {
|
|
||||||
return buckets[typ]
|
|
||||||
}
|
|
||||||
|
|
||||||
type PutRet struct {
|
type PutRet struct {
|
||||||
Hash string `json:"hash"`
|
Hash string `json:"hash"`
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
@@ -40,31 +33,19 @@ func onProgress(fsize, uploaded int64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func upload(typ string, filepath string) {
|
func Upload(name string, size int64, data io.Reader) (string, error) {
|
||||||
bucket := getBucket(typ)
|
if setting.Conf.Kodo.AccessKey == "" || setting.Conf.Kodo.SecretKey == "" {
|
||||||
if bucket == nil {
|
return "", errors.New("qiniu config error")
|
||||||
logd.Debug("invalid type:", typ)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Open(filepath)
|
conf.ACCESS_KEY = setting.Conf.Kodo.AccessKey
|
||||||
if err != nil {
|
conf.SECRET_KEY = setting.Conf.Kodo.SecretKey
|
||||||
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
|
|
||||||
// 创建一个client
|
// 创建一个client
|
||||||
c := kodo.New(0, nil)
|
c := kodo.New(0, nil)
|
||||||
|
|
||||||
// 设置上传的策略
|
// 设置上传的策略
|
||||||
policy := &kodo.PutPolicy{
|
policy := &kodo.PutPolicy{
|
||||||
Scope: bucket.name,
|
Scope: setting.Conf.Kodo.Name,
|
||||||
Expires: 3600,
|
Expires: 3600,
|
||||||
InsertOnly: 1,
|
InsertOnly: 1,
|
||||||
}
|
}
|
||||||
@@ -75,18 +56,30 @@ func upload(typ string, filepath string) {
|
|||||||
zone := 0
|
zone := 0
|
||||||
uploader := kodocli.NewUploader(zone, nil)
|
uploader := kodocli.NewUploader(zone, nil)
|
||||||
|
|
||||||
var ret PutRet
|
ext := filepath.Ext(name)
|
||||||
key := fmt.Sprintf("%s-%s%s", typ, chksum, ext)
|
var key string
|
||||||
|
switch ext {
|
||||||
fmt.Printf("Uploading .....")
|
case ".bmp", ".png", ".jpg", ".gif", ".ico":
|
||||||
var extra = kodocli.PutExtra{OnProgress: onProgress}
|
key = "blog/img/" + name
|
||||||
res := uploader.PutFile(nil, &ret, token, key, filepath, &extra)
|
case ".mov", ".mp4":
|
||||||
// 打印返回的信息
|
key = "blog/video/" + name
|
||||||
if res != nil {
|
case ".go", ".js", ".css", ".cpp", ".php", ".rb", ".java", ".py", ".sql", ".lua", ".html", ".sh", ".xml", ".cs":
|
||||||
logd.Debugf("failed to upload patch file: %v\n", res)
|
key = "blog/code/" + name
|
||||||
return
|
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)
|
var ret PutRet
|
||||||
fmt.Printf("url: %s\n", url)
|
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
21
qiniu_test.go
Normal 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
13
static/feed.xml
Executable 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
6
static/opensearch.xml
Executable 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
16
static/sitemap.xml
Executable 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>
|
||||||
Reference in New Issue
Block a user