Skip to content

Web server that serves currently active image #1887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@
[submodule "Submodules/spacecommander"]
path = Submodules/spacecommander
url = [email protected]:square/spacecommander.git
[submodule "Submodules/CocoaHTTPServer"]
path = Submodules/CocoaHTTPServer
url = https://github.com/robbiehanson/CocoaHTTPServer.git
243 changes: 240 additions & 3 deletions Project/LooseLeaf.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Project/LooseLeaf/CocoaAsyncSocket.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The CocoaAsyncSocket project is under Public Domain license.
http://code.google.com/p/cocoaasyncsocket/

The AsyncSocket project has been around since 2001 and is used in many applications and frameworks.
18 changes: 18 additions & 0 deletions Project/LooseLeaf/CocoaHTTPServer.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CocoaHTTPServer Software License Agreement (BSD License)

Copyright (c) 2011, Deusty, LLC
All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Neither the name of Deusty nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of Deusty, LLC.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 changes: 17 additions & 0 deletions Project/LooseLeaf/CocoaLumberjack.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CocoaLumberjack is under the New BSD License.
https://github.com/robbiehanson/CocoaLumberjack

BSD 3-Clause License

Copyright (c) 2010-2020, Deusty, LLC
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of Deusty nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Deusty, LLC.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions Project/LooseLeaf/MMAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import "MMUnknownObject.h"
#import "MMAllStacksManager.h"
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "MMPaperRequest.h"

@import AppCenter;
@import AppCenterAnalytics;
@import AppCenterCrashes;
Expand All @@ -42,6 +47,7 @@ @implementation MMAppDelegate {
@synthesize isActive = isActive;

static BOOL isFirstLaunch = NO;
static HTTPServer* httpServer;

+ (void)load {
NSString* uuid = [SSKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"userID"];
Expand All @@ -53,6 +59,15 @@ + (void)load {
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
DebugLog(@"Documents path: %@", [NSFileManager documentsPath]);

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHTTP"]) {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
httpServer = [[HTTPServer alloc] init];
[httpServer setPort:8088];
[httpServer setDocumentRoot:[NSFileManager documentsPath]];
[httpServer setConnectionClass:[MMPaperRequest class]];
NSError* error = nil;
[httpServer start:&error];
}
NSString* email = [[NSUserDefaults standardUserDefaults] stringForKey:kPendingEmailToSubscribe];

if (email) {
Expand Down Expand Up @@ -141,6 +156,10 @@ - (void)applicationWillResignActive:(UIApplication*)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
DebugLog(@"WILL RESIGN ACTIVE");
if (httpServer) {
[httpServer stop];
httpServer = nil;
}
isActive = NO;
[[MMRotationManager sharedInstance] willResignActive];
[self.viewController willResignActive];
Expand Down
1 change: 1 addition & 0 deletions Project/LooseLeaf/MMEditablePaperView.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- (void)setEditable:(BOOL)isEditable;
- (BOOL)isEditable;
- (void)cancelCurrentStrokeIfAny;
- (NSString*)exportAt;

// abstract
- (void)saveToDisk:(void (^)(BOOL didSaveEdits))onComplete;
Expand Down
3 changes: 3 additions & 0 deletions Project/LooseLeaf/MMEditablePaperView.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ - (void)cancelCurrentStrokeIfAny {
}
}

- (NSString*)exportAt {
return [self thumbnailPath];
}

#pragma mark - Ruler Tool

Expand Down
17 changes: 17 additions & 0 deletions Project/LooseLeaf/MMPaperRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MMPaperRequest.h
// LooseLeaf
//
// Created by Yih-Chun Hu on 10/11/20.
// Copyright © 2020 Milestone Made, LLC. All rights reserved.
//

#import "HTTPConnection.h"

NS_ASSUME_NONNULL_BEGIN

@interface MMPaperRequest : HTTPConnection
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path;
@end

NS_ASSUME_NONNULL_END
42 changes: 42 additions & 0 deletions Project/LooseLeaf/MMPaperRequest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// MMPaperRequest.m
// LooseLeaf
//
// Created by Yih-Chun Hu on 10/11/20.
// Copyright © 2020 Milestone Made, LLC. All rights reserved.
//

#import "MMPaperRequest.h"
#import "HTTPLogging.h"
#import "HTTPServer.h"
#import "HTTPConnection.h"
#import "HTTPMessage.h"
#import "HTTPResponse.h"
#import "HTTPAuthenticationRequest.h"
#import "HTTPFileResponse.h"
#import "HTTPAsyncFileResponse.h"
#import "MMPageCacheManager.h"

static const int httpLogLevel = HTTP_LOG_LEVEL_WARN;

@implementation MMPaperRequest
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path;
{
HTTPLogTrace();

// Override me to provide custom responses.

NSString *filePath = [self filePathForURI:path allowDirectory:NO];
if ([path isEqualToString:@"/"]) {
if ([MMPageCacheManager sharedInstance].currentEditablePage)
filePath = [[MMPageCacheManager sharedInstance].currentEditablePage exportAt];
}
BOOL isDir = NO;
if (filePath && [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) {
return [[HTTPFileResponse alloc] initWithFilePath:filePath forConnection:self];
}

return nil;
}

@end
16 changes: 16 additions & 0 deletions Project/LooseLeaf/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
<string>shellScript = &quot;cd \&quot;$SRCROOT/LooseLeaf\&quot;\n./generateLicenses.pl\ntouch Settings.bundle&quot;;</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Web Server Options</string>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>Enable Web Server</string>
<key>Key</key>
<string>enableHTTP</string>
<key>DefaultValue</key>
<false/>
</dict>
<dict>
<key>FooterText</key>
<string>Thanks</string>
Expand Down
1 change: 1 addition & 0 deletions Submodules/CocoaHTTPServer
Submodule CocoaHTTPServer added at 52b2a6