|
|
- package Util
-
- import (
- "fmt"
- "os"
- )
-
- // WriteFile to disk
- func WriteFile(contents []byte) (string, error) {
- var (
- fileName string
- filePath string
- cwd string
- f *os.File
- err error
- )
-
- cwd, err = os.Getwd()
- if err != nil {
- return fileName, err
- }
-
- fileName = RandomString(32)
-
- filePath = fmt.Sprintf(
- "%s/attachments/%s",
- cwd,
- fileName,
- )
-
- f, err = os.Create(filePath)
-
- if err != nil {
- return fileName, err
- }
-
- defer f.Close()
-
- _, err = f.Write(contents)
-
- if err != nil {
- return fileName, err
- }
-
- return fileName, nil
- }
|