Skip to content

Commit

Permalink
Merge pull request #425 from ToddLa/upload-feedback
Browse files Browse the repository at this point in the history
Show Server upload feedback on the device.
  • Loading branch information
yoshisuga authored Oct 11, 2022
2 parents ecf05bf + 8f1977c commit 50d08a8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
14 changes: 13 additions & 1 deletion iOS/EmulatorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -5859,6 +5859,17 @@ - (void)webServerDidCompleteBonjourRegistration:(GCDWebServer*)server {
[self webServerShowAlert:server];
}

- (void)webUploader:(GCDWebUploader*)uploader didUploadFile:(NSString*)file progress:(float)progress {
NSLog(@"FILE UPLOAD: %@ (%d%%)", file, (int)(progress * 100));
UIAlertController* alert = (UIAlertController*)self.topViewController;
if ([alert isKindOfClass:[UIAlertController class]])
[alert setProgress:progress text:file];
}

- (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path {
NSLog(@"FILE UPLOADED: %@", path);
}

- (void)webServerShowAlert:(GCDWebServer*)server {
// dont bring up this WebServer alert multiple times, for example the server will stop and restart when app goes into background.
static BOOL g_web_server_alert = FALSE;
Expand All @@ -5875,14 +5886,15 @@ - (void)webServerShowAlert:(GCDWebServer*)server {
[servers appendString:@"\n\n"];
}
if ( server.bonjourServerURL != nil ) {
[servers appendString:[NSString stringWithFormat:@"%@",server.bonjourServerURL]];
[servers appendString:[NSString stringWithFormat:@"%@\n\n",server.bonjourServerURL]];
}
NSString* welcome = @"Welcome to " PRODUCT_NAME_LONG;
NSString* message = [NSString stringWithFormat:@"To transfer ROMs from your computer go to one of these addresses in your web browser:\n\n%@",servers];
NSString* title = g_no_roms_found ? welcome : @"Web Server Started";
NSString* done = g_no_roms_found ? @"Reload ROMs" : @"Stop Server";

UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert setProgress:0.0 text:@""];
[alert addAction:[UIAlertAction actionWithTitle:done style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
g_web_server_alert = FALSE;
[[WebServer sharedInstance] webUploader].delegate = nil;
Expand Down
5 changes: 5 additions & 0 deletions xcode/MAME4iOS/GCDWebUploader/GCDWebUploader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
*/
- (void)webUploader:(GCDWebUploader*)uploader didDownloadFileAtPath:(NSString*)path;

/**
* This method is called while a file is being uploaded.
*/
- (void)webUploader:(GCDWebUploader*)uploader didUploadFile:(NSString*)fileName progress:(float)progress;

/**
* This method is called whenever a file has been uploaded.
*/
Expand Down
54 changes: 49 additions & 5 deletions xcode/MAME4iOS/GCDWebUploader/GCDWebUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ @interface GCDWebUploader () {
}
@end

GCDWebUploader* __unsafe_unretained g_server;

@implementation GCDWebUploader (Methods)

// Must match implementation in GCDWebDAVServer
Expand Down Expand Up @@ -283,6 +285,48 @@ - (GCDWebServerResponse*)createDirectory:(GCDWebServerURLEncodedFormRequest*)req

@end

@interface GCDWebServerMultiPartFormRequestWithProgress : GCDWebServerMultiPartFormRequest
@end
@implementation GCDWebServerMultiPartFormRequestWithProgress {
NSInteger _totalBytes;
NSString* _fileName;
}
- (BOOL)open:(NSError**)error {
_totalBytes = 0;
_fileName = nil;
return [super open:error];
}

- (BOOL)writeData:(NSData*)data error:(NSError**)error {
BOOL result = [super writeData:data error:error];

if (_totalBytes == 0 && _fileName == nil) {
@try {
// HACK: grab the fileName from the internal parser state, after we process the first data (aka header)
// we could parse the data ourself, but this is quick and we have a fallback.
_fileName = [self valueForKeyPath:@"_parser._fileName"];
}
@catch (id exception) {
_fileName = @"";
}
}

_totalBytes += data.length;

if ([g_server.delegate respondsToSelector:@selector(webUploader:didUploadFile:progress:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[g_server.delegate webUploader:g_server didUploadFile:self->_fileName progress:(float)self->_totalBytes / self.contentLength];
});
}

return result;
}

- (BOOL)close:(NSError**)error {
return [super close:error];
}
@end

@implementation GCDWebUploader

@synthesize uploadDirectory=_uploadDirectory, allowedFileExtensions=_allowedExtensions, allowHiddenItems=_allowHidden,
Expand All @@ -303,6 +347,7 @@ - (instancetype)initWithUploadDirectory:(NSString*)path {
#else
__block GCDWebUploader* server = self;
#endif
g_server = self;

// Resource files
[self addGETHandlerForBasePath:@"/" directoryPath:[siteBundle resourcePath] indexFilename:nil cacheAge:3600 allowRangeRequests:NO];
Expand Down Expand Up @@ -378,7 +423,7 @@ - (instancetype)initWithUploadDirectory:(NSString*)path {
}];

// File upload
[self addHandlerForMethod:@"POST" path:@"/upload" requestClass:[GCDWebServerMultiPartFormRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
[self addHandlerForMethod:@"POST" path:@"/upload" requestClass:[GCDWebServerMultiPartFormRequestWithProgress class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
return [server uploadFile:(GCDWebServerMultiPartFormRequest*)request];
}];

Expand All @@ -401,9 +446,9 @@ - (instancetype)initWithUploadDirectory:(NSString*)path {
return self;
}

#if !__has_feature(objc_arc)

- (void)dealloc {
g_server = nil;
#if !__has_feature(objc_arc)
[_uploadDirectory release];
[_allowedExtensions release];
[_title release];
Expand All @@ -413,9 +458,8 @@ - (void)dealloc {
[_footer release];

[super dealloc];
}

#endif
}

@end

Expand Down

2 comments on commit 50d08a8

@BruceWayen66
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find anyone's Email on GitHub, so I asked here, I've been working with iOS emulator(IFBA) recently.

  1. Could this emulator project run on iOS?
  2. Besides, https://github.com/yoyofr/iFBA, could this old project run on modern iOS? (it was 10 years ago built with armv7).
    Reply here, or You're welcome to contact me: [email protected]
    Thank you.

@BruceWayen66
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question 1 might be ambiguous, [this emulator project] refers to NAME4iOS.

Please sign in to comment.