代码拉取完成,页面将自动刷新
package mutil
import (
"fmt"
"io"
"mime/multipart"
"os"
"path"
)
var FileUpload = &upload{}
type upload struct {
}
func (domain *upload) LocalStorage(multipartFile multipart.File, basePath string, storageFilename string) (writePath string, errs error) {
fileBytes, err := io.ReadAll(multipartFile)
if err != nil {
return "", err
}
_, err = os.Stat(basePath)
if os.IsNotExist(err) {
err = os.MkdirAll(basePath, 0666)
if err != nil {
return "", err
}
} else if os.IsExist(err) {
return "", err
}
writePath = path.Join(basePath, storageFilename)
return writePath, os.WriteFile(writePath, fileBytes, 0666)
}
func (domain *upload) LocalStorageNewName(multipartFile multipart.File, basePath string, storageFilename string) error {
fileBytes, err := io.ReadAll(multipartFile)
if err != nil {
return err
}
dateStr := DateUtil.NowDateTime().Format(DateUtil.DateFormatNumber)
filepath := path.Join(basePath, dateStr)
_, err = os.Stat(filepath)
if os.IsNotExist(err) {
err = os.MkdirAll(filepath, 0666)
if err != nil {
return err
}
}
for true {
filename := fmt.Sprintf("%s%s", RandomUtil.UniqueId(fmt.Sprintf("%s-", RandomUtil.NextXAZ(1)), 26), path.Ext(storageFilename))
err = os.WriteFile(path.Join(filepath, filename), fileBytes, 0666)
if os.IsExist(err) {
continue
}
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。