Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 6m27s
feat: 添加版本和构建时间变量 fix: 修正短信队列类型注释错误 style: 清理未使用的代码和测试文件 docs: 更新安装文档中的下载链接 chore: 迁移数据库脚本添加日志和订阅配置
42 lines
993 B
Go
42 lines
993 B
Go
package fs
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/perfect-panel/server/pkg/hash"
|
|
)
|
|
|
|
// TempFileWithText creates the temporary file with the given content,
|
|
// and returns the opened *os.File instance.
|
|
// The file is kept as open, the caller should close the file handle,
|
|
// and remove the file by name.
|
|
func TempFileWithText(text string) (*os.File, error) {
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text)))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tmpFile, nil
|
|
}
|
|
|
|
// TempFilenameWithText creates the file with the given content,
|
|
// and returns the filename (full path).
|
|
// The caller should remove the file after use.
|
|
func TempFilenameWithText(text string) (string, error) {
|
|
tmpFile, err := TempFileWithText(text)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
filename := tmpFile.Name()
|
|
if err = tmpFile.Close(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filename, nil
|
|
}
|