From ed88fb8c80ab7695e21c21eadeb98c36dd7f7477 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 6 Aug 2008 16:17:50 -0400 Subject: [PATCH] Use the original file name instead of the __bu_print_drop__.pdf * Source/AppController.m: (getUploadSafeName:): New method to get an upload-safe name (uploadAndPrint:): Use the new upload-safe name rather than the hard-coded one --- Source/AppController.m | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Source/AppController.m b/Source/AppController.m index d8952e4..5169133 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -19,14 +19,15 @@ #import #import #include +#include @interface AppController (Private) - - (void)setStatus:(NSString *)msg isError:(BOOL)error; - (void)readChannel:(LIBSSH2_CHANNEL *)channel; - (void)uploadAndPrint:(id)sender; +- (NSString *)getUploadSafeName:(NSString *)name; @end @@ -154,6 +155,7 @@ end: FILE *localFile; struct stat fileInfo; + const char *fileName = [[self getUploadSafeName:[dragRegion filePath]] UTF8String]; [self setStatus:@"Connecting to acs.bu.edu" isError:NO]; struct sockaddr_in sin; @@ -191,7 +193,7 @@ end: goto shutdown; } - LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh, "~/__bu_print_drop__.pdf", 0755, (unsigned long)fileInfo.st_size); + LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh, fileName, 0755, (unsigned long)fileInfo.st_size); if (!channel) { [self setStatus:@"Unable to open upload SCP session" isError:YES]; @@ -254,7 +256,7 @@ end: // read the banner [self readChannel:channel]; - // f!cking ACS messages --> skip some more + // ACS outage messages --> skip some more char *delay = "q\r\n\0"; libssh2_channel_write(channel, delay, sizeof(char) * strlen(delay)); [self readChannel:channel]; @@ -263,15 +265,15 @@ end: char *cmd; #ifndef BLU_DEBUG NSString *printer = [[printersController selection] valueForKey:@"unixName"]; - cmd = (char *)[[NSString stringWithFormat:@"lpr -m -P%@ __bu_print_drop__.pdf\r\n\0", printer] UTF8String]; + cmd = (char *)[[NSString stringWithFormat:@"lpr -m -P%@ %s\r\n\0", printer, fileName] UTF8String]; #else - cmd = "touch abc.def\r\n\0"; + cmd = "touch __PRINT__\r\n\0"; #endif libssh2_channel_write(channel, cmd, sizeof(char) * strlen(cmd)); [self readChannel:channel]; // remove our temp file - cmd = "rm -f __bu_print_drop__.pdf\r\n\0"; + cmd = (char *)[[NSString stringWithFormat:@"rm -f %s\r\n\0", fileName] UTF8String]; libssh2_channel_write(channel, cmd, sizeof(char) * strlen(cmd)); [self readChannel:channel]; @@ -297,4 +299,21 @@ shutdown: [pool release]; } +/** + * Returns an NSString that is safe for uploding onto the server + */ +- (NSString *)getUploadSafeName:(NSString *)name +{ + NSMutableString *n = [NSMutableString stringWithString:name]; + [n replaceOccurrencesOfString:@" " withString:@"" options:NSBackwardsSearch range:NSMakeRange(0, [name length])]; + name = (NSString *)n; + name = [name lastPathComponent]; + name = [name stringByDeletingPathExtension]; + if ([name length] > 20) + name = [name substringToIndex:20]; + + srandomdev(); + return [NSString stringWithFormat:@"~/%@.%d.pdf", name, random()]; +} + @end -- 2.22.5