Skip to content

Commit

Permalink
only allow overwrite on success
Browse files Browse the repository at this point in the history
  • Loading branch information
iosdeveloper committed Feb 14, 2016
1 parent ad1e871 commit f291ff7
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions App/BitBar/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface AppDelegate : NSObject <NSApplicationDelegate, NSURLDownloadDelegate>
// plugin download
@property NSURLDownload *download;
@property NSString *destinationPath;
@property NSString *suggestedDestinationPath;

@end

Expand Down Expand Up @@ -107,6 +108,7 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppl

[self.download cancel];
self.destinationPath = nil;
self.suggestedDestinationPath = nil;

// NSURLSession is not available below 10.9 :(
self.download = [[NSURLDownload alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URLString]] delegate:self];
Expand All @@ -115,26 +117,39 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppl
#pragma mark - NSURLDownload delegate

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename {
[download setDestination:[DEFS.pluginsDirectory stringByAppendingPathComponent:filename] allowOverwrite:YES];
self.suggestedDestinationPath = [DEFS.pluginsDirectory stringByAppendingPathComponent:filename];
[download setDestination:self.suggestedDestinationPath allowOverwrite:NO];
}

- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path {
self.destinationPath = path;
}

- (void)downloadDidFinish:(NSURLDownload *)download {
if (!self.destinationPath)
return;

// ensure plugin is executable

// `chmod +x plugin.sh`
struct stat st;
stat(self.destinationPath.UTF8String, &st);
chmod(self.destinationPath.UTF8String, (st.st_mode & ALLPERMS) | S_IXUSR | S_IXGRP | S_IXOTH);
if (self.destinationPath) {
if (self.suggestedDestinationPath && ![self.suggestedDestinationPath isEqualToString:self.destinationPath]) {
// overwrite file at suggested destination path

[[NSFileManager defaultManager] removeItemAtPath:self.suggestedDestinationPath error:nil];

if ([[NSFileManager defaultManager] moveItemAtPath:self.destinationPath toPath:self.suggestedDestinationPath error:nil])
self.destinationPath = self.suggestedDestinationPath;
}

// ensure plugin is executable

// `chmod +x plugin.sh`
struct stat st;
stat(self.destinationPath.UTF8String, &st);
chmod(self.destinationPath.UTF8String, (st.st_mode & ALLPERMS) | S_IXUSR | S_IXGRP | S_IXOTH);
}

// refresh
[self.pluginManager reset];

self.download = nil;
self.destinationPath = nil;
self.suggestedDestinationPath = nil;
}

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
Expand All @@ -143,6 +158,10 @@ - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
alert.messageText = @"Download failed";
alert.informativeText = error.localizedDescription;
[alert runModal];

self.download = nil;
self.destinationPath = nil;
self.suggestedDestinationPath = nil;
}

@end
Expand Down

0 comments on commit f291ff7

Please sign in to comment.