mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-11-22 16:23:46 +00:00
36 lines
880 B
Go
36 lines
880 B
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func MakeLocal(filename, username string, buf bytes.Buffer) error {
|
|
if _, err := os.Stat(fmt.Sprintf("/tmp/tmpfile-%s-%s", filename, username)); !os.IsNotExist(err) {
|
|
return fmt.Errorf("file %s already exists: %w", filename, err)
|
|
}
|
|
|
|
defer buf.Reset()
|
|
|
|
f, err := os.Create("/tmp/tmpfile-" + filename + "-" + username) //nolint: gosec
|
|
// this is set to nolint because I am doing everything os.CreateTemp does but here since I don't like some of the limitations
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write file locally: %w", err)
|
|
}
|
|
|
|
// close and remove the temporary file at the end of the program
|
|
defer func() {
|
|
if err = f.Close(); err != nil {
|
|
return
|
|
}
|
|
}()
|
|
|
|
err = os.WriteFile(f.Name(), buf.Bytes(), 0o600)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write file locally: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|