Files
open-im-server/pkg/utils/file.go
T

78 lines
2.3 KiB
Go
Raw Normal View History

2021-05-26 19:35:56 +08:00
package utils
2022-03-23 15:44:34 +08:00
import (
2022-03-23 17:40:51 +08:00
"Open_IM/pkg/common/constant"
2022-05-10 10:44:43 +08:00
"errors"
2022-03-23 15:44:34 +08:00
"fmt"
"math/rand"
"os"
2022-03-23 17:40:51 +08:00
"path"
2022-03-23 15:44:34 +08:00
"time"
)
2021-05-26 19:35:56 +08:00
// Determine whether the given path is a folder
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// Determine whether the given path is a file
func IsFile(path string) bool {
return !IsDir(path)
}
// Create a directory
func MkDir(path string) error {
return os.MkdirAll(path, os.ModePerm)
}
2022-03-23 15:44:34 +08:00
2022-03-23 17:40:51 +08:00
func GetNewFileNameAndContentType(fileName string, fileType int) (string, string) {
suffix := path.Ext(fileName)
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileName)
2022-03-23 15:44:34 +08:00
contentType := ""
2022-03-23 17:40:51 +08:00
if fileType == constant.ImageType {
contentType = "image/" + suffix[1:]
2022-03-23 15:44:34 +08:00
}
return newName, contentType
}
2022-05-10 10:44:43 +08:00
2022-05-10 12:30:00 +08:00
func GetUploadAppNewName(appType int, version, fileName, yamlName string) (string, string, error) {
var newFileName, newYamlName = "_" + version + "_app", "_" + version + "_yaml"
2022-05-10 10:44:43 +08:00
switch appType {
case constant.IOSPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.IOSPlatformStr + newFileName
newYamlName = constant.IOSPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.AndroidPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.AndroidPlatformStr + newFileName
newYamlName = constant.AndroidPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.WindowsPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.WindowsPlatformStr + newFileName
newYamlName = constant.WindowsPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.OSXPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.OSXPlatformStr + newFileName
newYamlName = constant.OSXPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.WebPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.WebPlatformStr + newFileName
newYamlName = constant.WebPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.MiniWebPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.MiniWebPlatformStr + newFileName
newYamlName = constant.MiniWebPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
case constant.LinuxPlatformID:
2022-05-10 12:05:02 +08:00
newFileName = constant.LinuxPlatformStr + newFileName
newYamlName = constant.LinuxPlatformStr + newYamlName
2022-05-10 10:44:43 +08:00
default:
return "", "", errors.New("invalid app type")
}
2022-05-10 12:30:00 +08:00
suffixFile := path.Ext(fileName)
suffixYaml := path.Ext(yamlName)
2022-05-10 13:06:13 +08:00
newFileName = fmt.Sprintf("%s%s", newFileName, suffixFile)
newYamlName = fmt.Sprintf("%s%s", newYamlName, suffixYaml)
2022-05-10 18:28:57 +08:00
if yamlName == "" {
newYamlName = ""
}
2022-05-10 10:44:43 +08:00
return newFileName, newYamlName, nil
}