Returns the sha256 hash of the file, its size and an error
(filepath string)
| 194 | |
| 195 | // Returns the sha256 hash of the file, its size and an error |
| 196 | func fileStats(filepath string) (*fileInfo, error) { |
| 197 | stat, err := os.Stat(filepath) |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | |
| 202 | f, err := os.Open(filepath) |
| 203 | if err != nil { |
| 204 | return nil, fmt.Errorf("can't open file to upload: %w", err) |
| 205 | } |
| 206 | |
| 207 | hash, _, err := cr_v1.SHA256(f) |
| 208 | if err != nil { |
| 209 | f.Close() |
| 210 | return nil, fmt.Errorf("generating digest: %w", err) |
| 211 | } |
| 212 | |
| 213 | // Since we have already iterated on the file to calculate the digest |
| 214 | // we need to rewind the file pointer |
| 215 | _, err = f.Seek(0, io.SeekStart) |
| 216 | if err != nil { |
| 217 | f.Close() |
| 218 | return nil, fmt.Errorf("rewinding file pointer: %w", err) |
| 219 | } |
| 220 | |
| 221 | return &fileInfo{filename: stat.Name(), digest: hash.String(), size: stat.Size(), r: f}, nil |
| 222 | } |
| 223 | |
| 224 | type Craftable interface { |
| 225 | Craft(ctx context.Context, value string) (*api.Attestation_Material, error) |
no test coverage detected