2021-02-22 17:27:13 +05:30
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ReadAllTempfile(r io.Reader) (tempfile *os.File, err error) {
|
2022-07-23 23:45:48 +05:30
|
|
|
tempfile, err = os.CreateTemp("", "gitlab-workhorse-read-all-tempfile")
|
2021-02-22 17:27:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Avoid leaking an open file if the function returns with an error
|
|
|
|
if err != nil {
|
|
|
|
tempfile.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := os.Remove(tempfile.Name()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(tempfile, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tempfile.Seek(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempfile, nil
|
|
|
|
}
|