ReadFile reads a file with a given limit
(path string, limit int64)
| 36 | |
| 37 | // ReadFile reads a file with a given limit |
| 38 | func ReadFile(path string, limit int64) ([]byte, error) { |
| 39 | if limit <= 0 { |
| 40 | return os.ReadFile(path) |
| 41 | } |
| 42 | |
| 43 | f, err := os.Open(path) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | defer f.Close() |
| 48 | |
| 49 | st, err := f.Stat() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | size := st.Size() |
| 55 | if limit > 0 && size > limit { |
| 56 | size = limit |
| 57 | } |
| 58 | |
| 59 | buf := bytes.NewBuffer(nil) |
| 60 | buf.Grow(int(size)) |
| 61 | _, err = io.Copy(buf, io.LimitReader(f, limit)) |
| 62 | |
| 63 | return buf.Bytes(), err |
| 64 | } |
| 65 | |
| 66 | // Copy copies a file to a destination path |
| 67 | func Copy(sourcePath string, targetPath string, overwrite bool) error { |