-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05ef75e
commit 25da298
Showing
5 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |