Try to add share code in download api.
This commit is contained in:
18
code/tool/cache/cache.go
vendored
18
code/tool/cache/cache.go
vendored
@ -3,7 +3,6 @@ package cache
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
@ -148,6 +147,18 @@ func (table *Table) SetDeleteCallback(f func(*Item)) {
|
||||
table.deleteCallback = f
|
||||
}
|
||||
|
||||
//带有panic恢复的方法
|
||||
func (table *Table) RunWithRecovery(f func()) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
//core.LOGGER.Error("异步任务错误: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
//执行函数
|
||||
f()
|
||||
}
|
||||
|
||||
//终结检查,被自调整的时间触发
|
||||
func (table *Table) checkExpire() {
|
||||
table.Lock()
|
||||
@ -197,7 +208,7 @@ func (table *Table) checkExpire() {
|
||||
table.cleanupInterval = smallestDuration
|
||||
if smallestDuration > 0 {
|
||||
table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
|
||||
go util.RunWithRecovery(table.checkExpire)
|
||||
go table.RunWithRecovery(table.checkExpire)
|
||||
})
|
||||
}
|
||||
table.Unlock()
|
||||
@ -387,8 +398,7 @@ func (table *Table) MostAccessed(count int64) []*Item {
|
||||
|
||||
// 打印日志
|
||||
func (table *Table) log(format string, v ...interface{}) {
|
||||
//TODO: 全局日志记录
|
||||
//LOGGER.Info(format, v...)
|
||||
//core.LOGGER.Info(format, v...)
|
||||
}
|
||||
|
||||
//新建一个缓存Table
|
||||
|
@ -286,7 +286,7 @@ func DownloadFile(
|
||||
if size >= 0 {
|
||||
ranges, err := ParseRange(rangeReq, size)
|
||||
if err != nil {
|
||||
panic(result.CustomWebResult(result.CODE_WRAPPER_RANGE_NOT_SATISFIABLE, "range header出错"))
|
||||
panic(result.CustomWebResult(result.RANGE_NOT_SATISFIABLE, "range header出错"))
|
||||
}
|
||||
if SumRangesSize(ranges) > size {
|
||||
// The total number of bytes in all the ranges
|
||||
@ -310,7 +310,7 @@ func DownloadFile(
|
||||
// be sent using the multipart/byteranges media type."
|
||||
ra := ranges[0]
|
||||
if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
|
||||
panic(result.CustomWebResult(result.CODE_WRAPPER_RANGE_NOT_SATISFIABLE, "range header出错"))
|
||||
panic(result.CustomWebResult(result.RANGE_NOT_SATISFIABLE, "range header出错"))
|
||||
}
|
||||
sendSize = ra.length
|
||||
code = http.StatusPartialContent
|
||||
|
@ -22,55 +22,61 @@ type CodeWrapper struct {
|
||||
}
|
||||
|
||||
var (
|
||||
CODE_WRAPPER_OK = &CodeWrapper{Code: "OK", HttpStatus: http.StatusOK, Description: "成功"}
|
||||
CODE_WRAPPER_BAD_REQUEST = &CodeWrapper{Code: "BAD_REQUEST", HttpStatus: http.StatusBadRequest, Description: "请求不合法"}
|
||||
CODE_WRAPPER_CAPTCHA_ERROR = &CodeWrapper{Code: "CAPTCHA_ERROR", HttpStatus: http.StatusBadRequest, Description: "验证码错误"}
|
||||
CODE_WRAPPER_NEED_CAPTCHA = &CodeWrapper{Code: "NEED_CAPTCHA", HttpStatus: http.StatusBadRequest, Description: "验证码必填"}
|
||||
CODE_WRAPPER_USERNAME_PASSWORD_ERROR = &CodeWrapper{Code: "USERNAME_PASSWORD_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
|
||||
CODE_WRAPPER_PARAMS_ERROR = &CodeWrapper{Code: "PARAMS_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
|
||||
CODE_WRAPPER_LOGIN = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "未登录,禁止访问"}
|
||||
CODE_WRAPPER_LOGIN_EXPIRE = &CodeWrapper{Code: "LOGIN_EXPIRE", HttpStatus: http.StatusUnauthorized, Description: "登录过期,请重新登录"}
|
||||
CODE_WRAPPER_USER_DISABLED = &CodeWrapper{Code: "USER_DISABLED", HttpStatus: http.StatusForbidden, Description: "账户被禁用,禁止访问"}
|
||||
CODE_WRAPPER_UNAUTHORIZED = &CodeWrapper{Code: "UNAUTHORIZED", HttpStatus: http.StatusUnauthorized, Description: "没有权限,禁止访问"}
|
||||
CODE_WRAPPER_NOT_FOUND = &CodeWrapper{Code: "NOT_FOUND", HttpStatus: http.StatusNotFound, Description: "内容不存在"}
|
||||
CODE_WRAPPER_RANGE_NOT_SATISFIABLE = &CodeWrapper{Code: "RANGE_NOT_SATISFIABLE", HttpStatus: http.StatusRequestedRangeNotSatisfiable, Description: "文件范围读取错误"}
|
||||
CODE_WRAPPER_NOT_INSTALLED = &CodeWrapper{Code: "NOT_INSTALLED", HttpStatus: http.StatusInternalServerError, Description: "系统尚未安装"}
|
||||
CODE_WRAPPER_SERVER = &CodeWrapper{Code: "SERVER", HttpStatus: http.StatusInternalServerError, Description: "服务器出错"}
|
||||
CODE_WRAPPER_UNKNOWN = &CodeWrapper{Code: "UNKNOWN", HttpStatus: http.StatusInternalServerError, Description: "服务器未知错误"}
|
||||
OK = &CodeWrapper{Code: "OK", HttpStatus: http.StatusOK, Description: "成功"}
|
||||
BAD_REQUEST = &CodeWrapper{Code: "BAD_REQUEST", HttpStatus: http.StatusBadRequest, Description: "请求不合法"}
|
||||
CAPTCHA_ERROR = &CodeWrapper{Code: "CAPTCHA_ERROR", HttpStatus: http.StatusBadRequest, Description: "验证码错误"}
|
||||
NEED_CAPTCHA = &CodeWrapper{Code: "NEED_CAPTCHA", HttpStatus: http.StatusBadRequest, Description: "验证码必填"}
|
||||
NEED_SHARE_CODE = &CodeWrapper{Code: "NEED_SHARE_CODE", HttpStatus: http.StatusUnauthorized, Description: "分享提取码必填"}
|
||||
SHARE_CODE_ERROR = &CodeWrapper{Code: "SHARE_CODE_ERROR", HttpStatus: http.StatusUnauthorized, Description: "分享提取码错误"}
|
||||
USERNAME_PASSWORD_ERROR = &CodeWrapper{Code: "USERNAME_PASSWORD_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
|
||||
PARAMS_ERROR = &CodeWrapper{Code: "PARAMS_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
|
||||
LOGIN = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "未登录,禁止访问"}
|
||||
LOGIN_EXPIRE = &CodeWrapper{Code: "LOGIN_EXPIRE", HttpStatus: http.StatusUnauthorized, Description: "登录过期,请重新登录"}
|
||||
USER_DISABLED = &CodeWrapper{Code: "USER_DISABLED", HttpStatus: http.StatusForbidden, Description: "账户被禁用,禁止访问"}
|
||||
UNAUTHORIZED = &CodeWrapper{Code: "UNAUTHORIZED", HttpStatus: http.StatusUnauthorized, Description: "没有权限,禁止访问"}
|
||||
NOT_FOUND = &CodeWrapper{Code: "NOT_FOUND", HttpStatus: http.StatusNotFound, Description: "内容不存在"}
|
||||
RANGE_NOT_SATISFIABLE = &CodeWrapper{Code: "RANGE_NOT_SATISFIABLE", HttpStatus: http.StatusRequestedRangeNotSatisfiable, Description: "文件范围读取错误"}
|
||||
NOT_INSTALLED = &CodeWrapper{Code: "NOT_INSTALLED", HttpStatus: http.StatusInternalServerError, Description: "系统尚未安装"}
|
||||
SERVER = &CodeWrapper{Code: "SERVER", HttpStatus: http.StatusInternalServerError, Description: "服务器出错"}
|
||||
UNKNOWN = &CodeWrapper{Code: "UNKNOWN", HttpStatus: http.StatusInternalServerError, Description: "服务器未知错误"}
|
||||
)
|
||||
|
||||
//根据 CodeWrapper来获取对应的HttpStatus
|
||||
func FetchHttpStatus(code string) int {
|
||||
if code == CODE_WRAPPER_OK.Code {
|
||||
return CODE_WRAPPER_OK.HttpStatus
|
||||
} else if code == CODE_WRAPPER_BAD_REQUEST.Code {
|
||||
return CODE_WRAPPER_BAD_REQUEST.HttpStatus
|
||||
} else if code == CODE_WRAPPER_CAPTCHA_ERROR.Code {
|
||||
return CODE_WRAPPER_CAPTCHA_ERROR.HttpStatus
|
||||
} else if code == CODE_WRAPPER_NEED_CAPTCHA.Code {
|
||||
return CODE_WRAPPER_NEED_CAPTCHA.HttpStatus
|
||||
} else if code == CODE_WRAPPER_USERNAME_PASSWORD_ERROR.Code {
|
||||
return CODE_WRAPPER_USERNAME_PASSWORD_ERROR.HttpStatus
|
||||
} else if code == CODE_WRAPPER_PARAMS_ERROR.Code {
|
||||
return CODE_WRAPPER_PARAMS_ERROR.HttpStatus
|
||||
} else if code == CODE_WRAPPER_LOGIN.Code {
|
||||
return CODE_WRAPPER_LOGIN.HttpStatus
|
||||
} else if code == CODE_WRAPPER_LOGIN_EXPIRE.Code {
|
||||
return CODE_WRAPPER_LOGIN_EXPIRE.HttpStatus
|
||||
} else if code == CODE_WRAPPER_USER_DISABLED.Code {
|
||||
return CODE_WRAPPER_USER_DISABLED.HttpStatus
|
||||
} else if code == CODE_WRAPPER_UNAUTHORIZED.Code {
|
||||
return CODE_WRAPPER_UNAUTHORIZED.HttpStatus
|
||||
} else if code == CODE_WRAPPER_NOT_FOUND.Code {
|
||||
return CODE_WRAPPER_NOT_FOUND.HttpStatus
|
||||
} else if code == CODE_WRAPPER_RANGE_NOT_SATISFIABLE.Code {
|
||||
return CODE_WRAPPER_RANGE_NOT_SATISFIABLE.HttpStatus
|
||||
} else if code == CODE_WRAPPER_NOT_INSTALLED.Code {
|
||||
return CODE_WRAPPER_NOT_INSTALLED.HttpStatus
|
||||
} else if code == CODE_WRAPPER_SERVER.Code {
|
||||
return CODE_WRAPPER_SERVER.HttpStatus
|
||||
if code == OK.Code {
|
||||
return OK.HttpStatus
|
||||
} else if code == BAD_REQUEST.Code {
|
||||
return BAD_REQUEST.HttpStatus
|
||||
} else if code == CAPTCHA_ERROR.Code {
|
||||
return CAPTCHA_ERROR.HttpStatus
|
||||
} else if code == NEED_CAPTCHA.Code {
|
||||
return NEED_CAPTCHA.HttpStatus
|
||||
} else if code == NEED_SHARE_CODE.Code {
|
||||
return NEED_SHARE_CODE.HttpStatus
|
||||
} else if code == SHARE_CODE_ERROR.Code {
|
||||
return SHARE_CODE_ERROR.HttpStatus
|
||||
} else if code == USERNAME_PASSWORD_ERROR.Code {
|
||||
return USERNAME_PASSWORD_ERROR.HttpStatus
|
||||
} else if code == PARAMS_ERROR.Code {
|
||||
return PARAMS_ERROR.HttpStatus
|
||||
} else if code == LOGIN.Code {
|
||||
return LOGIN.HttpStatus
|
||||
} else if code == LOGIN_EXPIRE.Code {
|
||||
return LOGIN_EXPIRE.HttpStatus
|
||||
} else if code == USER_DISABLED.Code {
|
||||
return USER_DISABLED.HttpStatus
|
||||
} else if code == UNAUTHORIZED.Code {
|
||||
return UNAUTHORIZED.HttpStatus
|
||||
} else if code == NOT_FOUND.Code {
|
||||
return NOT_FOUND.HttpStatus
|
||||
} else if code == RANGE_NOT_SATISFIABLE.Code {
|
||||
return RANGE_NOT_SATISFIABLE.HttpStatus
|
||||
} else if code == NOT_INSTALLED.Code {
|
||||
return NOT_INSTALLED.HttpStatus
|
||||
} else if code == SERVER.Code {
|
||||
return SERVER.HttpStatus
|
||||
} else {
|
||||
return CODE_WRAPPER_UNKNOWN.HttpStatus
|
||||
return UNKNOWN.HttpStatus
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,6 +91,9 @@ func ConstWebResult(codeWrapper *CodeWrapper) *WebResult {
|
||||
|
||||
func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult {
|
||||
|
||||
if description == "" {
|
||||
description = codeWrapper.Description
|
||||
}
|
||||
wr := &WebResult{
|
||||
Code: codeWrapper.Code,
|
||||
Msg: description,
|
||||
@ -94,23 +103,23 @@ func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult {
|
||||
|
||||
//请求参数有问题
|
||||
func BadRequest(format string, v ...interface{}) *WebResult {
|
||||
return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...))
|
||||
return CustomWebResult(BAD_REQUEST, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
//没有权限
|
||||
func Unauthorized(format string, v ...interface{}) *WebResult {
|
||||
return CustomWebResult(CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...))
|
||||
return CustomWebResult(UNAUTHORIZED, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
//没有找到
|
||||
func NotFound(format string, v ...interface{}) *WebResult {
|
||||
return CustomWebResult(CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...))
|
||||
return CustomWebResult(NOT_FOUND, fmt.Sprintf(format, v...))
|
||||
|
||||
}
|
||||
|
||||
//服务器内部出问题
|
||||
func Server(format string, v ...interface{}) *WebResult {
|
||||
return CustomWebResult(CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...))
|
||||
return CustomWebResult(SERVER, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
//所有的数据库错误情况
|
||||
|
Reference in New Issue
Block a user