mirror of
https://github.com/qvacua/vimr.git
synced 2024-11-28 02:54:31 +03:00
GH-339 Use shareFile
This commit is contained in:
parent
1a7f633cea
commit
12e64039e7
@ -16,6 +16,7 @@
|
||||
1929B4145AA81F006BAF3B5C /* PreviewService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BB8BCA48637156F92945 /* PreviewService.swift */; };
|
||||
1929B462CD4935AFF6D69457 /* FileItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B7CB4863F80230C32D3C /* FileItem.swift */; };
|
||||
1929B53876E6952D378C2B30 /* ScoredFileItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BDF9EBAF1D9D44399045 /* ScoredFileItem.swift */; };
|
||||
1929B5CF6ECCBCC3FB5292CE /* HttpServerService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B0D767ED19EE1281ECD9 /* HttpServerService.swift */; };
|
||||
1929B6388EAF16C190B82955 /* FileItemIgnorePattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B69499B2569793350CEC /* FileItemIgnorePattern.swift */; };
|
||||
1929B67DA3EB21A631EF1DBB /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BA8AC40B901B20F20B71 /* FileUtils.swift */; };
|
||||
1929B728262BAA14FC93F6AC /* NeoVimView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BF00B466B40629C2AABE /* NeoVimView.swift */; };
|
||||
@ -281,6 +282,7 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
1929B02440BC99C42F9EBD45 /* NetUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetUtils.m; sourceTree = "<group>"; };
|
||||
1929B0D767ED19EE1281ECD9 /* HttpServerService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpServerService.swift; sourceTree = "<group>"; };
|
||||
1929B0EEBE4A765934AF8335 /* DataWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DataWrapper.h; sourceTree = "<group>"; };
|
||||
1929B15B7EDC9B0F40E5E95C /* Logging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Logging.h; sourceTree = "<group>"; };
|
||||
1929B1A51F076E088EF4CCA4 /* server_globals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = server_globals.h; sourceTree = "<group>"; };
|
||||
@ -812,6 +814,7 @@
|
||||
4BDF50101D760AB700D8FBC3 /* Commons */,
|
||||
4B6423941D8EFD6100FC78C8 /* Workspace */,
|
||||
4B97E2CF1D33F92200FC0660 /* resources */,
|
||||
1929B0D767ED19EE1281ECD9 /* HttpServerService.swift */,
|
||||
);
|
||||
path = VimR;
|
||||
sourceTree = "<group>";
|
||||
@ -1306,6 +1309,7 @@
|
||||
1929B4145AA81F006BAF3B5C /* PreviewService.swift in Sources */,
|
||||
1929BD52275A6570C666A7BA /* PreviewRenderer.swift in Sources */,
|
||||
1929BD2F41D93ADFF43C1C98 /* NetUtils.m in Sources */,
|
||||
1929B5CF6ECCBCC3FB5292CE /* HttpServerService.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
14
VimR/HttpServerService.swift
Normal file
14
VimR/HttpServerService.swift
Normal file
@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by Tae Won Ha on 1/13/17.
|
||||
// Copyright (c) 2017 Tae Won Ha. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Swifter
|
||||
|
||||
class HttpServerService {
|
||||
|
||||
fileprivate let server = HttpServer()
|
||||
|
||||
|
||||
}
|
@ -42,12 +42,26 @@ fileprivate class WebviewMessageHandler: NSObject, WKScriptMessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
|
||||
return { r in
|
||||
guard let file = try? path.openForReading() else {
|
||||
return .notFound
|
||||
}
|
||||
|
||||
return .raw(200, "OK", [:], { writer in
|
||||
try? writer.write(file)
|
||||
file.close()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
|
||||
static let identifier = "com.qvacua.vimr.tool.preview.markdown"
|
||||
static func prefData(from dict: [String: Any]) -> StandardPrefData? {
|
||||
return PrefData(dict: dict)
|
||||
}
|
||||
static let serverPath = "tool/preview/markdown"
|
||||
|
||||
struct PrefData: StandardPrefData {
|
||||
|
||||
@ -134,6 +148,11 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
fileprivate var currentUrl: URL?
|
||||
fileprivate var currentPreviewPosition = Position(row: 1, column: 1)
|
||||
|
||||
fileprivate let uuid = UUID().uuidString
|
||||
fileprivate var server = HttpServer()
|
||||
fileprivate let port: in_port_t
|
||||
fileprivate let tempDir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
|
||||
let identifier: String = MarkdownRenderer.identifier
|
||||
var prefData: StandardPrefData? {
|
||||
return PrefData(isForwardSearchAutomatically: self.isForwardSearchAutomatically,
|
||||
@ -152,9 +171,6 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
let toolbar: NSView? = NSView(forAutoLayout: ())
|
||||
let menuItems: [NSMenuItem]?
|
||||
|
||||
fileprivate var server = HttpServer()
|
||||
fileprivate let port: in_port_t
|
||||
|
||||
init(source: Observable<Any>, scrollSource: Observable<Any>, initialData: PrefData) {
|
||||
guard let templateUrl = Bundle.main.url(forResource: "template",
|
||||
withExtension: "html",
|
||||
@ -167,12 +183,17 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
preconditionFailure("ERROR Cannot load markdown template")
|
||||
}
|
||||
|
||||
self.server["/preview/markdown/:path"] = shareFilesFromDirectory("/Users/hat/Downloads")
|
||||
let css = (try? String(contentsOf: self.resourceBaesUrl.appendingPathComponent("github-markdown.css"))) ?? ""
|
||||
self.server.GET["/preview/markdown/github-markdown.css"] = { arg in .ok(.html(css)) }
|
||||
self.server.GET["/\(MarkdownRenderer.serverPath)/github-markdown.css"] = shareFile(
|
||||
self.resourceBaesUrl.appendingPathComponent("github-markdown.css").path
|
||||
)
|
||||
|
||||
self.port = NetUtils.openPort()
|
||||
NSLog("opening a server on port \(port)")
|
||||
do { try self.server.start(port) } catch { NSLog("!!!!!!!!!!!!!!!!!!!!!!!") }
|
||||
NSLog("\(#function): opening a server on port \(port)")
|
||||
do {
|
||||
try self.server.start(port)
|
||||
} catch {
|
||||
NSLog("ERROR \(#function): could not start the http server on port \(self.port)")
|
||||
}
|
||||
|
||||
self.template = template
|
||||
|
||||
@ -353,7 +374,6 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
|
||||
fileprivate func filledTemplate(body: String, title: String) -> String {
|
||||
return self.template
|
||||
.replacingOccurrences(of: "{{ resource-base-path }}", with: self.resourceBaesUrl.path)
|
||||
.replacingOccurrences(of: "{{ title }}", with: title)
|
||||
.replacingOccurrences(of: "{{ body }}", with: body)
|
||||
}
|
||||
@ -368,15 +388,23 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
|
||||
}
|
||||
|
||||
let html = filledTemplate(body: body, title: url.lastPathComponent)
|
||||
try? html.write(toFile: "/tmp/markdown-preview.html", atomically: false, encoding: .utf8)
|
||||
let htmlFilePath = tempDir.appendingPathComponent("\(MarkdownRenderer.identifier).\(self.uuid).html").path
|
||||
do {
|
||||
try html.write(toFile: htmlFilePath, atomically: true, encoding: .utf8)
|
||||
} catch {
|
||||
NSLog("ERROR \(#function): could not write preview file to \(htmlFilePath)")
|
||||
self.flow.publish(event: PreviewRendererAction.error(renderer: self))
|
||||
return
|
||||
}
|
||||
|
||||
let baseUrl = url.deletingLastPathComponent()
|
||||
NSLog("baseUrl: \(baseUrl)")
|
||||
self.server["/\(MarkdownRenderer.serverPath)/:path"] = shareFilesFromDirectory(url.deletingLastPathComponent().path)
|
||||
self.server.GET["/\(MarkdownRenderer.serverPath)/index.html"] = shareFile(htmlFilePath)
|
||||
|
||||
self.server.GET["/preview/markdown/index.html"] = { arg in .ok(.html(html)) }
|
||||
let urlRequest = URLRequest(
|
||||
url: URL(string: "http://localhost:\(self.port)/\(MarkdownRenderer.serverPath)/index.html")!
|
||||
)
|
||||
self.webview.load(urlRequest)
|
||||
|
||||
let url = URL(string: "http://localhost:\(self.port)/preview/markdown/index.html")!
|
||||
self.webview.load(URLRequest(url: url))
|
||||
self.flow.publish(event: PreviewRendererAction.view(renderer: self, view: self.webview))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user