You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/utils/file.go

78 lines
2.3 KiB

package utils
3 years ago
import (
3 years ago
"Open_IM/pkg/common/constant"
3 years ago
"errors"
3 years ago
"fmt"
"math/rand"
"os"
3 years ago
"path"
3 years ago
"time"
)
// 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)
}
3 years ago
3 years ago
func GetNewFileNameAndContentType(fileName string, fileType int) (string, string) {
suffix := path.Ext(fileName)
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileName)
3 years ago
contentType := ""
3 years ago
if fileType == constant.ImageType {
contentType = "image/" + suffix[1:]
3 years ago
}
return newName, contentType
}
3 years ago
3 years ago
func GetUploadAppNewName(appType int, version, fileName, yamlName string) (string, string, error) {
var newFileName, newYamlName = "_" + version + "_app", "_" + version + "_yaml"
3 years ago
switch appType {
case constant.IOSPlatformID:
3 years ago
newFileName = constant.IOSPlatformStr + newFileName
newYamlName = constant.IOSPlatformStr + newYamlName
3 years ago
case constant.AndroidPlatformID:
3 years ago
newFileName = constant.AndroidPlatformStr + newFileName
newYamlName = constant.AndroidPlatformStr + newYamlName
3 years ago
case constant.WindowsPlatformID:
3 years ago
newFileName = constant.WindowsPlatformStr + newFileName
newYamlName = constant.WindowsPlatformStr + newYamlName
3 years ago
case constant.OSXPlatformID:
3 years ago
newFileName = constant.OSXPlatformStr + newFileName
newYamlName = constant.OSXPlatformStr + newYamlName
3 years ago
case constant.WebPlatformID:
3 years ago
newFileName = constant.WebPlatformStr + newFileName
newYamlName = constant.WebPlatformStr + newYamlName
3 years ago
case constant.MiniWebPlatformID:
3 years ago
newFileName = constant.MiniWebPlatformStr + newFileName
newYamlName = constant.MiniWebPlatformStr + newYamlName
3 years ago
case constant.LinuxPlatformID:
3 years ago
newFileName = constant.LinuxPlatformStr + newFileName
newYamlName = constant.LinuxPlatformStr + newYamlName
3 years ago
default:
return "", "", errors.New("invalid app type")
}
3 years ago
suffixFile := path.Ext(fileName)
suffixYaml := path.Ext(yamlName)
3 years ago
newFileName = fmt.Sprintf("%s%s", newFileName, suffixFile)
newYamlName = fmt.Sprintf("%s%s", newYamlName, suffixYaml)
3 years ago
if yamlName == "" {
newYamlName = ""
}
3 years ago
return newFileName, newYamlName, nil
}