BufferInFile is a convenience function which creates FileBuffer with underlying file created in the specified directory with the random name.
(r io.Reader, dir string)
| 63 | // BufferInFile is a convenience function which creates FileBuffer with underlying |
| 64 | // file created in the specified directory with the random name. |
| 65 | func BufferInFile(r io.Reader, dir string) (Buffer, error) { |
| 66 | // It is assumed that PRNG is initialized somewhere during program startup. |
| 67 | nameBytes := make([]byte, 32) |
| 68 | _, err := rand.Read(nameBytes) |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf("buffer: failed to generate randomness for file name: %v", err) |
| 71 | } |
| 72 | path := filepath.Join(dir, hex.EncodeToString(nameBytes)) |
| 73 | f, err := os.Create(path) |
| 74 | if err != nil { |
| 75 | return nil, fmt.Errorf("buffer: failed to create file: %v", err) |
| 76 | } |
| 77 | if _, err = io.Copy(f, r); err != nil { |
| 78 | return nil, fmt.Errorf("buffer: failed to write file: %v", err) |
| 79 | } |
| 80 | if err := f.Close(); err != nil { |
| 81 | return nil, fmt.Errorf("buffer: failed to close file: %v", err) |
| 82 | } |
| 83 | |
| 84 | return FileBuffer{Path: path}, nil |
| 85 | } |
no test coverage detected