Skip to content

Instantly share code, notes, and snippets.

@thusoy
Created November 20, 2018 00:38
Show Gist options
  • Save thusoy/b0c1c2c80bc56d60f0c17cfb8ff94aca to your computer and use it in GitHub Desktop.
Save thusoy/b0c1c2c80bc56d60f0c17cfb8ff94aca to your computer and use it in GitHub Desktop.
Delete all megacool recordings
- (void)deleteAllRecordings {
int count = 0;
NSFileManager *fileManager = NSFileManager.defaultManager;
NSURL *appSupport =
[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask][0];
NSURL *megacoolAppSupport = [appSupport URLByAppendingPathComponent:@"Megacool" isDirectory:YES];
NSError *error;
NSArray<NSURL *> *recordingDirectories = [fileManager
contentsOfDirectoryAtURL:megacoolAppSupport
includingPropertiesForKeys:nil
options:0
error:&error];
if (!recordingDirectories) {
NSLog(@"Couldn't list Megacool recordings: %@", error.localizedDescription);
return;
}
for (NSURL *recordingDirectory in recordingDirectories) {
// All recording directories are a hex hash of the recordingId
if (recordingDirectory.lastPathComponent.length != 32) {
NSLog(@"Not deleting non-recording directory %@", recordingDirectory.absoluteString);
continue;
}
NSString *recordingId = @"UNKNOWN";
// Not necessary, but parse the details of the recording to log which recording it was
NSURL *recordingStateFile = [recordingDirectory URLByAppendingPathComponent:@"state.json"];
NSData *binaryState = [fileManager contentsAtPath:recordingStateFile.path];
if (!binaryState) {
NSLog(@"Failed to load state for recording at %@, deleting anyway", recordingStateFile);
} else {
NSDictionary *state = [NSJSONSerialization JSONObjectWithData:binaryState options:0 error:&error];
if (!state) {
NSLog(@"Could not deserialize recording state: %@", error.localizedDescription);
} else {
recordingId = state[@"recordingId"];
}
}
NSLog(@"Deleting recording '%@' at %@", recordingId, recordingDirectory.absoluteString);
if (![fileManager removeItemAtURL:recordingDirectory error:&error] &&
error.code != NSFileNoSuchFileError) {
NSLog(@"Failed to delete recording '%@': %@", recordingId, error.localizedDescription);
} else {
count += 1;
}
}
NSLog(@"Deleted %d recordings", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment