diff --git a/testdata/ffmpeg.d/cmd/prune/main.go b/testdata/ffmpeg.d/cmd/prune/main.go index 0262415..26c1605 100644 --- a/testdata/ffmpeg.d/cmd/prune/main.go +++ b/testdata/ffmpeg.d/cmd/prune/main.go @@ -109,6 +109,46 @@ func Run(ctx context.Context, args []string) error { } } } + + for _, cam := range cams { + camMovementD := strings.ReplaceAll(cam, "record", "movement") + + movementFiles, err := lsf(camMovementD) + if err != nil { + return err + } + sort.Strings(movementFiles) + + var sizeOfCamDMiB int64 + for _, movementFile := range movementFiles { + stat, err := os.Stat(movementFile) + if err != nil { + return err + } + fileSizeMiB := stat.Size() / 1024 / 1024 + sizeOfCamDMiB += fileSizeMiB + } + + GiB100 := int64(100 * 1024) + for sizeOfCamDMiB > GiB100 && len(movementFiles) > 0 { + oldestFile := movementFiles[0] + + oldestFileStat, err := os.Stat(oldestFile) + if err != nil { + return err + } + oldestFileSizeMiB := oldestFileStat.Size() / 1024 / 1024 + + log.Println("deleting old file", oldestFile) + if err := os.Remove(oldestFile); err != nil { + return err + } + + sizeOfCamDMiB -= oldestFileSizeMiB + movementFiles = movementFiles[1:] + } + } + return nil }