Use the original file name instead of the __bu_print_drop__.pdf
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 6 Aug 2008 20:17:50 +0000 (16:17 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 6 Aug 2008 20:17:50 +0000 (16:17 -0400)
* 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

index d8952e4db3d433a79afe41d4e8b4d77621ca232c..5169133521f51f4317c166fd7a2c6b7608e03e71 100644 (file)
 #import <sys/socket.h>
 #import <arpa/inet.h>
 #include <netdb.h>
+#include <stdlib.h>
 
 @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