Documentation ¶
Index ¶
- func Base64Decode(s string) ([]byte, error)
- func Base64Encode(data []byte) string
- func BytesToThumb(img []byte, thumbPath string) error
- func CheckErr(w http.ResponseWriter, err error, code int) bool
- func CheckErrorFatal(err error)
- func CheckErrorPanic(err error)
- func CreateFile(filePath string, src io.Reader) error
- func CreateReturnFile(filePath string, src io.Reader) (int64, *os.File, error)
- func DeleteFiles(files ...string) error
- func ErrorContains(err error, substr string) bool
- func GetFileContents(r *http.Request) ([]byte, error)
- func GetFilesByExt(dir, ext string) ([]string, error)
- func GetFormValue(w http.ResponseWriter, r *http.Request, key string) (value string, ok bool)
- func GetID(w http.ResponseWriter, r *http.Request) (string, bool)
- func HasString(slice []string, item string) bool
- func HttpGet(url string, cookies []*http.Cookie) (*http.Response, error)
- func HttpPostForm(url string, data url.Values, cookies []*http.Cookie) (*http.Response, error)
- func JsonMessage(w http.ResponseWriter, message string, code int)
- func JsonMsg404(w http.ResponseWriter)
- func JsonMsgOK(w http.ResponseWriter)
- func JsonRequireLogin(w http.ResponseWriter)
- func JsonResponse(w http.ResponseWriter, obj interface{}, code int)
- func MustMkdir(dirName string)
- func NewID() string
- func NoCaseContains(s, substr string) bool
- func PathIsExist(name string) bool
- func PathIsNotExist(name string) bool
- func Sha256Hex(data []byte) string
- func StringIndex(slice []string, item string) int
- func TimeNow(format string) string
- func TimestampFilename(ext string) string
- func TypeByFilename(filename string) string
- func UserHomeDir() string
- func WrapErrors(allErrors ...error) (wrapped error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToThumb ¶
BytesToThumb creates a thumbnail from img, uses default size and default quality, and write the thumbnail to thumbPath.
func CheckErr ¶
func CheckErr(w http.ResponseWriter, err error, code int) bool
CheckErr 检查 err, 如果有错就以 json 形式返回给前端,并返回 true. 如果没有错误则返回 false.
func CreateFile ¶
CreateFile 把 src 的数据写入 filePath, 权限是 0600, 自动关闭 file.
func CreateReturnFile ¶
CreateReturnFile 把 src 的数据写入 filePath, 权限是 0600, 会自动创建或覆盖文件,返回 file, 要记得关闭资源。
func DeleteFiles ¶
DeleteFiles 删除全部 files, 忽略找不到文件的错误,返回其他错误。
func ErrorContains ¶
ErrorContains returns NoCaseContains(err.Error(), substr) Returns false if err is nil.
func GetFileContents ¶
GetFileContents gets contents from http.Request.FormFile("file"). It also verifies the file has not been corrupted.
func GetFormValue ¶
GetFormValue checks if the r.FormValue(key) is empty or not, if it is empty, write error message and return false; if it is not empty, return the id and true.
func HttpPostForm ¶
HttpPostForm issues a POST to the specified URL with cookies, The Content-Type is set to "application/x-www-form-urlencoded".
func JsonMessage ¶
func JsonMessage(w http.ResponseWriter, message string, code int)
JsonMessage 主要用于向前端返回出错消息。
func JsonResponse ¶
func JsonResponse(w http.ResponseWriter, obj interface{}, code int)
JsonResponse 要用于向前端返回有用数据。 参考 https://stackoverflow.com/questions/59763852/can-you-return-json-in-golang-http-error
func MustMkdir ¶
func MustMkdir(dirName string)
MustMkdir 确保有一个名为 dirName 的文件夹, 如果没有则自动创建,如果已存在则不进行任何操作。
func NewID ¶
func NewID() string
NewID 返回一个上升趋势的随机 id, 由时间戳与随机数组成。 时间戳确保其上升趋势(大致有序),随机数确保其随机性(防止被穷举)。 NewID 考虑了 “生成新 id 的速度”、 “并发生成时防止冲突” 与 “id 长度” 这三者的平衡,适用于大多数中、小规模系统(当然,不适用于大型系统)。
func NoCaseContains ¶
NoCaseContains reports whether substr is within s case-insensitive.
func StringIndex ¶
StringIndex returns the index of item in the slice. returns -1 if not found.
func TypeByFilename ¶
TypeByFilename 从文件名中截取后缀名,然后判断文件类型。
Types ¶
This section is empty.