Skip to content

Commit

Permalink
增加保存元信息代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lingjiao0710 committed May 20, 2019
1 parent 05ef75e commit 25da298
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
Go实战仿百度云盘 实现企业级分布式云存储系统
Go实战仿百度云盘 实现企业级分布式云存储系统

```go
go run main.go
```

使用浏览器访问:http://localhost:8080/file/upload

16 changes: 14 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"io/ioutil"
"os"
"fmt"
"time"
"github.com/lingjiao0710/filestore-server/meta"
"github.com/lingjiao0710/filestore-server/util"
)

//UploadHandler: 处理文件上传
Expand All @@ -28,21 +31,30 @@ func UploadHandler(w http.ResponseWriter, r *http.Request){

defer file.Close()

fileMeta := meta.FileMeta{
FileName: head.Filename,
Location: "./" + head.Filename,
UploadAt: time.Now().Format("2006-01-02 15:04:05"),
}

//创建本地文件
newfile, err := os.Create("./" + head.Filename)
newfile, err := os.Create(fileMeta.Location)
if err != nil{
fmt.Printf("creat file failed, err:%s\n", err.Error())
return
}
defer newfile.Close()

//复制数据
_, err = io.Copy(newfile, file)
fileMeta.Filesize, err = io.Copy(newfile, file)
if err != nil {
fmt.Printf("save data failed, err:%s\n", err.Error())
return
}

newfile.Seek(0, 0)
fileMeta.FileSha1 = util.FileSha1(newfile)
meta.UpdateFileMeta(fileMeta)
//重定向到suc路由
http.Redirect(w, r, "/file/upload/suc", http.StatusFound)
}
Expand Down
29 changes: 29 additions & 0 deletions meta/filemeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package meta

//FileMeta: 文件元信息结构
type FileMeta struct {
FileSha1 string
FileName string
Filesize int64
Location string
UploadAt string
}


var fileMetas map[string]FileMeta

//初始化fileMetas
func init() {
fileMetas = make(map[string]FileMeta)
}

//UpdateFileMeta: 新增、更新文件元信息
func UpdateFileMeta(fmeta FileMeta) {
fileMetas[fmeta.FileSha1] = fmeta
}

//GetFileMeta: 通过sha1获取文件元信息

func GetFileMeta(fileSha1 string) FileMeta{
return fileMetas[fileSha1]
}
51 changes: 51 additions & 0 deletions util/resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package util

import (
"encoding/json"
"fmt"
"log"
)

// RespMsg : http响应数据的通用结构
type RespMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}

// NewRespMsg : 生成response对象
func NewRespMsg(code int, msg string, data interface{}) *RespMsg {
return &RespMsg{
Code: code,
Msg: msg,
Data: data,
}
}

// JSONBytes : 对象转json格式的二进制数组
func (resp *RespMsg) JSONBytes() []byte {
r, err := json.Marshal(resp)
if err != nil {
log.Println(err)
}
return r
}

// JSONString : 对象转json格式的string
func (resp *RespMsg) JSONString() string {
r, err := json.Marshal(resp)
if err != nil {
log.Println(err)
}
return string(r)
}

// GenSimpleRespStream : 只包含code和message的响应体([]byte)
func GenSimpleRespStream(code int, msg string) []byte {
return []byte(fmt.Sprintf(`{"code":%d,"msg":"%s"}`, code, msg))
}

// GenSimpleRespString : 只包含code和message的响应体(string)
func GenSimpleRespString(code int, msg string) string {
return fmt.Sprintf(`{"code":%d,"msg":"%s"}`, code, msg)
}
70 changes: 70 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package util

import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"hash"
"io"
"os"
"path/filepath"
)

type Sha1Stream struct {
_sha1 hash.Hash
}

func (obj *Sha1Stream) Update(data []byte) {
if obj._sha1 == nil {
obj._sha1 = sha1.New()
}
obj._sha1.Write(data)
}

func (obj *Sha1Stream) Sum() string {
return hex.EncodeToString(obj._sha1.Sum([]byte("")))
}

func Sha1(data []byte) string {
_sha1 := sha1.New()
_sha1.Write(data)
return hex.EncodeToString(_sha1.Sum([]byte("")))
}

func FileSha1(file *os.File) string {
_sha1 := sha1.New()
io.Copy(_sha1, file)
return hex.EncodeToString(_sha1.Sum(nil))
}

func MD5(data []byte) string {
_md5 := md5.New()
_md5.Write(data)
return hex.EncodeToString(_md5.Sum([]byte("")))
}

func FileMD5(file *os.File) string {
_md5 := md5.New()
io.Copy(_md5, file)
return hex.EncodeToString(_md5.Sum(nil))
}

func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func GetFileSize(filename string) int64 {
var result int64
filepath.Walk(filename, func(path string, f os.FileInfo, err error) error {
result = f.Size()
return nil
})
return result
}

0 comments on commit 25da298

Please sign in to comment.