From 06118327277b6488a62d48f1471366565be0109f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 10 Oct 2020 20:36:58 +0200 Subject: Start a Python SimpleHTTPServer in dropped file directory When a file or directory is dropped on the drop zone, start a Python SimpleHTTPServer from that directory. Ensure the server is terminated when the Drop Serve application quits. --- Drop Serve.xcodeproj/project.pbxproj | 6 ++++++ Drop Serve/AppDelegate.m | 7 +++++++ Drop Serve/DropZone.m | 9 ++++++--- Drop Serve/Server.h | 18 ++++++++++++++++++ Drop Serve/Server.m | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 Drop Serve/Server.h create mode 100644 Drop Serve/Server.m diff --git a/Drop Serve.xcodeproj/project.pbxproj b/Drop Serve.xcodeproj/project.pbxproj index e9641f7..ecbf232 100644 --- a/Drop Serve.xcodeproj/project.pbxproj +++ b/Drop Serve.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ D1CA9B0C253211050013B767 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D1CA9B0B253211050013B767 /* AppDelegate.m */; }; D1CA9B0F253211050013B767 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D1CA9B0D253211050013B767 /* MainMenu.xib */; }; D1CA9B17253215150013B767 /* DropZone.m in Sources */ = {isa = PBXBuildFile; fileRef = D1CA9B16253215150013B767 /* DropZone.m */; }; + D1CA9B1E253230C60013B767 /* Server.m in Sources */ = {isa = PBXBuildFile; fileRef = D1CA9B1D253230C60013B767 /* Server.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -32,6 +33,8 @@ D1CA9B0E253211050013B767 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainMenu.xib; sourceTree = ""; }; D1CA9B15253215150013B767 /* DropZone.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DropZone.h; sourceTree = ""; }; D1CA9B16253215150013B767 /* DropZone.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DropZone.m; sourceTree = ""; }; + D1CA9B1C253230C60013B767 /* Server.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Server.h; sourceTree = ""; }; + D1CA9B1D253230C60013B767 /* Server.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Server.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -89,6 +92,8 @@ D1CA9B0B253211050013B767 /* AppDelegate.m */, D1CA9B15253215150013B767 /* DropZone.h */, D1CA9B16253215150013B767 /* DropZone.m */, + D1CA9B1C253230C60013B767 /* Server.h */, + D1CA9B1D253230C60013B767 /* Server.m */, D1CA9B0D253211050013B767 /* MainMenu.xib */, D1CA9AFF253211040013B767 /* Supporting Files */, ); @@ -174,6 +179,7 @@ D1CA9B05253211050013B767 /* main.m in Sources */, D1CA9B0C253211050013B767 /* AppDelegate.m in Sources */, D1CA9B17253215150013B767 /* DropZone.m in Sources */, + D1CA9B1E253230C60013B767 /* Server.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Drop Serve/AppDelegate.m b/Drop Serve/AppDelegate.m index f92f421..4a72685 100644 --- a/Drop Serve/AppDelegate.m +++ b/Drop Serve/AppDelegate.m @@ -8,6 +8,8 @@ #import "AppDelegate.h" +#import "Server.h" + @implementation AppDelegate - (void)dealloc @@ -19,4 +21,9 @@ { } +- (void)applicationWillTerminate:(NSNotification *)notification +{ + [Server stop]; +} + @end diff --git a/Drop Serve/DropZone.m b/Drop Serve/DropZone.m index 79c1fe6..56efb08 100644 --- a/Drop Serve/DropZone.m +++ b/Drop Serve/DropZone.m @@ -7,6 +7,7 @@ // #import "DropZone.h" +#import "Server.h" @implementation DropZone @@ -61,13 +62,15 @@ return NO; } - NSURL *path = [NSURL fileURLWithPath:file isDirectory:isDirectory]; + NSURL *url = [NSURL fileURLWithPath:file isDirectory:isDirectory]; if (!isDirectory) { - path = [path URLByDeletingLastPathComponent]; + url = [url URLByDeletingLastPathComponent]; } - NSLog(@"%@", path); + NSLog(@"%@", url); + + [Server serveAtPath:[url path]]; } return YES; diff --git a/Drop Serve/Server.h b/Drop Serve/Server.h new file mode 100644 index 0000000..a19908f --- /dev/null +++ b/Drop Serve/Server.h @@ -0,0 +1,18 @@ +// +// Server.h +// Drop Serve +// +// Created by TW on 10/10/20. +// Copyright (c) 2020 TW. All rights reserved. +// + +#import + +NSTask *_process; + +@interface Server : NSObject + ++ (void)serveAtPath:(NSString *)path; ++ (void)stop; + +@end diff --git a/Drop Serve/Server.m b/Drop Serve/Server.m new file mode 100644 index 0000000..c584f3c --- /dev/null +++ b/Drop Serve/Server.m @@ -0,0 +1,33 @@ +// +// Server.m +// Drop Serve +// +// Created by TW on 10/10/20. +// Copyright (c) 2020 TW. All rights reserved. +// + +#import "Server.h" + +@implementation Server + ++ (void)serveAtPath:(NSString *)path +{ + _process = [[NSTask alloc] init]; + [_process setCurrentDirectoryPath:path]; + [_process setLaunchPath:@"/usr/bin/python"]; + [_process setArguments:[NSArray arrayWithObjects: + @"-m", + @"SimpleHTTPServer", + nil]]; + + NSLog(@"%@", _process); + + [_process launch]; +} + ++ (void)stop +{ + [_process interrupt]; +} + +@end -- cgit v1.2.3