初始化项目文件
This commit is contained in:
319
api_file/service/api/file.go
Normal file
319
api_file/service/api/file.go
Normal file
@ -0,0 +1,319 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/sirupsen/logrus"
|
||||
"main/database"
|
||||
"main/model"
|
||||
"main/utils"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getRootFile(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
userPath := fmt.Sprintf("./upload/%s", username.Username)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
err := os.MkdirAll(userPath, 0755)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
var files filesRes
|
||||
fileList, err := os.ReadDir(userPath)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
for _, file := range fileList {
|
||||
if file.IsDir() {
|
||||
files.Dirs = append(files.Dirs, file.Name())
|
||||
} else {
|
||||
var item fileItem
|
||||
item.Name = file.Name()
|
||||
f, err := os.Stat(path.Join(userPath, file.Name()))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
item.Size = f.Size()
|
||||
item.Type = utils.GetFileType(f.Name())
|
||||
files.Files = append(files.Files, item)
|
||||
}
|
||||
}
|
||||
err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", files))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 获取指定目录下文件
|
||||
func getFile(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username.Username, filePath)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New(userPath + ":目录不存在"))
|
||||
return
|
||||
}
|
||||
var files filesRes
|
||||
fileList, err := os.ReadDir(userPath)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
for _, file := range fileList {
|
||||
if file.IsDir() {
|
||||
files.Dirs = append(files.Dirs, file.Name())
|
||||
} else {
|
||||
var item fileItem
|
||||
item.Name = file.Name()
|
||||
f, err := os.Stat(path.Join(userPath, file.Name()))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
item.Size = f.Size()
|
||||
item.Type = utils.GetFileType(f.Name())
|
||||
files.Files = append(files.Files, item)
|
||||
}
|
||||
}
|
||||
err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", files))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文件或目录
|
||||
func deleteFile(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username.Username, filePath)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("目录不存在"))
|
||||
return
|
||||
}
|
||||
if info, _ := os.Stat(userPath); info.IsDir() {
|
||||
err := os.RemoveAll(userPath)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := os.Remove(userPath)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
err := ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 上传头像
|
||||
func uploadAvatar(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
file, info, err := ctx.FormFile("file")
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
defer func(file multipart.File) {
|
||||
err = file.Close()
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}(file)
|
||||
userPath := fmt.Sprintf("./static/%s", username.Username)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
err = os.MkdirAll(userPath, 0755)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
fileType := strings.Split(info.Filename, ".")[len(strings.Split(info.Filename, "."))-1]
|
||||
avatarName := fmt.Sprintf("./static/%s/avatar-%s.%s", username.Username, time.Now().Format("2006-01-02"), fileType)
|
||||
_, err = ctx.SaveFormFile(info, avatarName)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", avatarName[1:]))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func uploadDir(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username.Username, filePath)
|
||||
if utils.FileIsExist(userPath) {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.SetErr(errors.New("目录已存在"))
|
||||
return
|
||||
}
|
||||
err := os.MkdirAll(userPath, 0755)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
func uploadFile(ctx iris.Context) {
|
||||
username := utils.GetLoginUser(ctx)
|
||||
if utils.DataIsNil(username) {
|
||||
return
|
||||
}
|
||||
filePath := ctx.Params().Get("path")
|
||||
file, info, err := ctx.FormFile("file")
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
defer func(file multipart.File) {
|
||||
err = file.Close()
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}(file)
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username.Username, filePath)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
err = os.MkdirAll(userPath, 0755)
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
_, err = ctx.SaveFormFile(info, fmt.Sprintf("./upload/%s/%s/%s", username.Username, filePath, info.Filename))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
|
||||
if utils.ErrHandle(ctx, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 文件下载,转到nginx-upload目录
|
||||
func downloadFile(ctx iris.Context) {
|
||||
authToken := ctx.GetCookie("token")
|
||||
activeTime := time.Now().Add(-2 * time.Hour)
|
||||
var userToken model.JwtKeys
|
||||
db := database.GetInstance().GetMysqlDb()
|
||||
if err := db.Where("token = ? and created_at >= ?", authToken, activeTime).First(&userToken).Error; err != nil {
|
||||
logrus.Errorln("sql执行失败:", err)
|
||||
}
|
||||
if utils.DataIsNil(userToken.Username) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("未登录"))
|
||||
return
|
||||
}
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", userToken.Username, filePath)
|
||||
if !utils.FileIsExist(userPath) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("文件不存在"))
|
||||
return
|
||||
}
|
||||
if info, _ := os.Stat(userPath); info.IsDir() {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("只可下载文件"))
|
||||
return
|
||||
}
|
||||
ctx.Recorder().Header().Add("X-Accel-Redirect", fmt.Sprintf("/upload/%s/%s", userToken.Username, filePath))
|
||||
ctx.Recorder().Header().Add("X-Accel-Charset", "utf-8")
|
||||
ctx.Recorder().Header().Add("Content-Disposition", "attachment")
|
||||
ctx.Recorder().Header().Add("Content-Type", "application/octet-stream; charset=utf-8")
|
||||
return
|
||||
}
|
||||
|
||||
func getDownloadFileType(ctx iris.Context) {
|
||||
authToken := ctx.GetCookie("token")
|
||||
activeTime := time.Now().Add(-2 * time.Hour)
|
||||
var userToken model.JwtKeys
|
||||
var res videoM3u8
|
||||
db := database.GetInstance().GetMysqlDb()
|
||||
if err := db.Where("token = ? and created_at >= ?", authToken, activeTime).First(&userToken).Error; err != nil {
|
||||
logrus.Errorln("sql执行失败:", err)
|
||||
}
|
||||
if utils.DataIsNil(userToken.Username) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("未登录"))
|
||||
return
|
||||
}
|
||||
username := userToken.Username
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username, filePath)
|
||||
currentPath, _ := filepath.Split(filePath)
|
||||
filename := strings.TrimSuffix(path.Base(userPath), path.Ext(userPath))
|
||||
res.Video = utils.GetFileType(userPath) == "video"
|
||||
res.M3u8 = utils.FileIsExist(fmt.Sprintf("./upload-video/%s/%s%s/%s.m3u8", username, currentPath, filename, filename))
|
||||
err := ctx.JSON(utils.FormatRes(iris.StatusOK, "", res))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func downloadVideo(ctx iris.Context) {
|
||||
authToken := ctx.GetCookie("token")
|
||||
activeTime := time.Now().Add(-2 * time.Hour)
|
||||
var userToken model.JwtKeys
|
||||
db := database.GetInstance().GetMysqlDb()
|
||||
if err := db.Where("token = ? and created_at >= ?", authToken, activeTime).First(&userToken).Error; err != nil {
|
||||
logrus.Errorln("sql执行失败:", err)
|
||||
}
|
||||
if utils.DataIsNil(userToken.Username) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("未登录"))
|
||||
return
|
||||
}
|
||||
username := userToken.Username
|
||||
filePath := ctx.Params().Get("path")
|
||||
userPath := fmt.Sprintf("./upload/%s/%s", username, filePath)
|
||||
currentPath, _ := filepath.Split(filePath)
|
||||
filename := strings.TrimSuffix(path.Base(userPath), path.Ext(userPath))
|
||||
//fmt.Println(filePath, currentPath, filename)
|
||||
if path.Ext(userPath) != ".ts" && !utils.FileIsExist(userPath) {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("文件不存在"))
|
||||
return
|
||||
}
|
||||
if info, err := os.Stat(userPath); err == nil && info.IsDir() {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.SetErr(errors.New("只可下载文件"))
|
||||
return
|
||||
}
|
||||
if utils.GetFileType(userPath) == "video" && utils.FileIsExist(fmt.Sprintf("./upload-video/%s/%s%s/%s.m3u8", username, currentPath, filename, filename)) {
|
||||
ctx.Recorder().Header().Add("X-Accel-Redirect", fmt.Sprintf("/upload-video/%s/%s%s/%s.m3u8", username, currentPath, filename, filename))
|
||||
} else if utils.GetFileType(userPath) == "video" {
|
||||
ctx.Recorder().Header().Add("X-Accel-Redirect", fmt.Sprintf("/upload/%s/%s", username, filePath))
|
||||
} else {
|
||||
tsPath := fmt.Sprintf("./upload-video/%s/%s%s/%s.ts", username, currentPath, filename[:len(filename)-6], filename)
|
||||
ctx.Recorder().Header().Add("X-Accel-Redirect", tsPath[1:])
|
||||
}
|
||||
ctx.Recorder().Header().Add("X-Accel-Charset", "utf-8")
|
||||
ctx.Recorder().Header().Add("Content-Disposition", "attachment")
|
||||
ctx.Recorder().Header().Add("Content-Type", "application/octet-stream; charset=utf-8")
|
||||
return
|
||||
}
|
17
api_file/service/api/fileExport.go
Normal file
17
api_file/service/api/fileExport.go
Normal file
@ -0,0 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func File(party iris.Party) {
|
||||
party.Get("/upload", getRootFile)
|
||||
party.Get("/upload/{path:path}", getFile)
|
||||
party.Post("/upload/{path:path}", uploadDir)
|
||||
party.Post("/upload-file/{path:path}", uploadFile)
|
||||
party.Delete("/upload/{path:path}", deleteFile)
|
||||
party.Post("/static/avatar", uploadAvatar)
|
||||
party.Get("/download/{path:path}", downloadFile)
|
||||
party.Get("/download-video-check/{path:path}", getDownloadFileType)
|
||||
party.Get("/download-video/{path:path}", downloadVideo)
|
||||
}
|
13
api_file/service/api/init.go
Normal file
13
api_file/service/api/init.go
Normal file
@ -0,0 +1,13 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"main/utils"
|
||||
)
|
||||
|
||||
func Apis(ctx iris.Context) {
|
||||
err := ctx.JSON(utils.FormatRes(iris.StatusOK, "", "allFileApis"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
172
api_file/service/api/type.d.go
Normal file
172
api_file/service/api/type.d.go
Normal file
@ -0,0 +1,172 @@
|
||||
package api
|
||||
|
||||
import "time"
|
||||
|
||||
// user-----------------------------------------------------------------------------------------------
|
||||
type result struct {
|
||||
Date string
|
||||
Password string
|
||||
ConfirmCode string
|
||||
UpdatedAt time.Time
|
||||
Key string
|
||||
AesKey string
|
||||
}
|
||||
|
||||
// weather---------------------------------------------------------------------------------------------
|
||||
type resWeather struct {
|
||||
Status int `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Data resData `json:"data"`
|
||||
}
|
||||
type resData struct {
|
||||
Forecast24h resDate `json:"forecast_24h"`
|
||||
}
|
||||
type resDate struct {
|
||||
D0 res24h `json:"1"`
|
||||
D1 res24h `json:"2"`
|
||||
}
|
||||
type res24h struct {
|
||||
Time string `json:"time"`
|
||||
MaxDegree string `json:"max_degree"`
|
||||
MinDegree string `json:"min_degree"`
|
||||
DayWeather string `json:"day_weather"`
|
||||
DayWindDirection string `json:"day_wind_direction"`
|
||||
DayWindPower string `json:"day_wind_power"`
|
||||
NightWeather string `json:"night_weather"`
|
||||
NightWindPower string `json:"night_wind_power"`
|
||||
NightWindDirection string `json:"night_wind_direction"`
|
||||
}
|
||||
type resLocation struct {
|
||||
Data map[string]string `json:"data"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// backgammon-----------------------------------------------------------------------------------------------
|
||||
type typeRoomStatus struct {
|
||||
RoomId int `json:"room_id"`
|
||||
Player string `json:"player"`
|
||||
}
|
||||
|
||||
// file-----------------------------------------------------------------------------------------------------
|
||||
type filesRes struct {
|
||||
Dirs []string `json:"dirs"`
|
||||
Files []fileItem `json:"files"`
|
||||
}
|
||||
type fileItem struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
type videoM3u8 struct {
|
||||
Video bool `json:"video"`
|
||||
M3u8 bool `json:"m3u8"`
|
||||
}
|
||||
|
||||
// notes------------------------------------------------------------------------------------------------------
|
||||
type noteParam struct {
|
||||
ID uint `json:"id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// sysInfo------------------------------------------------------------------------------------------------------
|
||||
type resSysInfo struct {
|
||||
Datetime string `json:"datetime"`
|
||||
CpuPer float64 `json:"cpu_per"`
|
||||
Mem resMem `json:"mem"`
|
||||
Disk []resDisk `json:"disk"`
|
||||
Net resNet `json:"net"`
|
||||
}
|
||||
type resMem struct {
|
||||
MemTotal string `json:"mem_total"`
|
||||
MemUsed string `json:"mem_used"`
|
||||
MemPer float64 `json:"mem_per"`
|
||||
}
|
||||
type resDisk struct {
|
||||
Point string `json:"point"`
|
||||
Total string `json:"total"`
|
||||
Used string `json:"used"`
|
||||
Per float64 `json:"per"`
|
||||
}
|
||||
type resNet struct {
|
||||
Sent string `json:"sent"`
|
||||
Rec string `json:"rec"`
|
||||
}
|
||||
type resSystem struct {
|
||||
Hostname string `json:"hostname"`
|
||||
Ip string `json:"ip"`
|
||||
}
|
||||
type sysInfoIDs struct {
|
||||
IDs []int `json:"ids"`
|
||||
}
|
||||
|
||||
// yeb----------------------------------------------------------------------------------------------------------
|
||||
type paramsBalance struct {
|
||||
Card string `json:"card"`
|
||||
Type bool `json:"type"`
|
||||
Balance float64 `json:"balance"`
|
||||
}
|
||||
|
||||
type paramSZBalance struct {
|
||||
Card string `json:"card"`
|
||||
Type bool `json:"type"` // 1-支出;0-收入
|
||||
Amount float64 `json:"amount"`
|
||||
}
|
||||
|
||||
// 月余额
|
||||
type dateLog struct {
|
||||
Date string `json:"date"`
|
||||
Duration string `json:"duration"`
|
||||
Changes float64 `json:"changes"`
|
||||
Balance float64 `json:"balance"`
|
||||
Detail []cardLog `json:"detail"`
|
||||
}
|
||||
|
||||
// 详情
|
||||
type cardLog struct {
|
||||
Card string `json:"card"`
|
||||
Balance float64 `json:"balance"`
|
||||
Changes float64 `json:"changes"`
|
||||
}
|
||||
|
||||
// menus-----------------------------------------------------------------------------
|
||||
type resMenu struct {
|
||||
MenuId string `json:"menu_id"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Path string `json:"path"`
|
||||
RouteOnly bool `json:"route_only"`
|
||||
Detail []subMenu `json:"detail"`
|
||||
}
|
||||
|
||||
type subMenu struct {
|
||||
MenuId string `json:"menu_id"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Path string `json:"path"`
|
||||
RouteOnly bool `json:"route_only"`
|
||||
}
|
||||
|
||||
type sysIconsParam struct {
|
||||
Icons string `json:"icons"`
|
||||
}
|
||||
|
||||
// sudoku-----------------------------------------------------------------------------
|
||||
type resSudokuListType struct {
|
||||
New []sudokuListType `json:"new"`
|
||||
Starting []sudokuListType `json:"starting"`
|
||||
Complete []sudokuListType `json:"complete"`
|
||||
}
|
||||
|
||||
type sudokuListType struct {
|
||||
Sudoku string `json:"sudoku"`
|
||||
SudokuId uint `json:"sudokuId"`
|
||||
Username string `json:"username"`
|
||||
Status string `json:"status"`
|
||||
Complete bool `json:"complete"`
|
||||
Id uint `json:"id"`
|
||||
}
|
||||
type addSudokuParam struct {
|
||||
Sudoku string `json:"sudoku"`
|
||||
Result string `json:"result"`
|
||||
}
|
Reference in New Issue
Block a user