package utils

import (
	"fmt"
	"github.com/astaxie/beego"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
	comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
	"os"
	"path"
	"time"
)

// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}

// 判断所给路径是否为文件夹
func IsDir(path string) bool {
	s, err := os.Stat(path)
	if err != nil {
		return false
	}
	return s.IsDir()
}

// 判断所给路径是否为文件
func IsFile(path string) bool {
	return !IsDir(path)
}

//fileType: video voice image
func GetFileName(projectName, fileType string, filename string) string {
	date := comm_time.GetTimeByYyyymmdd()
	subfix := path.Ext(filename)
	if len(projectName) == 0 {
		projectName = beego.BConfig.AppName
	}
	prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
	filename = fmt.Sprintf("%v%v", prefix, subfix)
	sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", projectName, beego.BConfig.RunMode, date, fileType, filename)
	return sourcePath
}