Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
guoye-zhang committed Nov 3, 2024
1 parent 5f4828e commit a85ca15
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Sources/NIOResumableUploadDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ final class UploadServerHandler: ChannelDuplexHandler {
case .head(let request):
switch request.method {
case .post, .put:
if let requestPath = request.path {
let url = self.directory.appending(path: requestPath, directoryHint: .notDirectory).standardized
if url.path(percentEncoded: false).hasPrefix(self.directory.path(percentEncoded: false)) {
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
FileManager.default.createFile(atPath: url.path(percentEncoded: false), contents: nil)
if let path = request.path {
let url = self.directory.appendingPathComponent(path, isDirectory: false).standardized
if url.path.hasPrefix(self.directory.path) {
try? FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
_ = FileManager.default.createFile(atPath: url.path, contents: nil)
self.fileHandle = try? FileHandle(forWritingTo: url)
print("Creating \(url)")
}
}
if self.fileHandle == nil {
let response = HTTPResponse(status: .internalServerError)
let response = HTTPResponse(status: .badRequest)
self.write(context: context, data: self.wrapOutboundOut(.head(response)), promise: nil)
self.write(context: context, data: self.wrapOutboundOut(.end(nil)), promise: nil)
self.flush(context: context)
Expand All @@ -70,11 +73,19 @@ final class UploadServerHandler: ChannelDuplexHandler {
exit(1)
}
case .end:
if fileHandle != nil {
let response = HTTPResponse(status: .created)
self.write(context: context, data: self.wrapOutboundOut(.head(response)), promise: nil)
self.write(context: context, data: self.wrapOutboundOut(.end(nil)), promise: nil)
self.flush(context: context)
if let fileHandle = self.fileHandle {
do {
try fileHandle.close()
let response = HTTPResponse(status: .created)
self.write(context: context, data: self.wrapOutboundOut(.head(response)), promise: nil)
self.write(context: context, data: self.wrapOutboundOut(.end(nil)), promise: nil)
self.flush(context: context)
} catch {
let response = HTTPResponse(status: .internalServerError)
self.write(context: context, data: self.wrapOutboundOut(.head(response)), promise: nil)
self.write(context: context, data: self.wrapOutboundOut(.end(nil)), promise: nil)
self.flush(context: context)
}
}
}
}
Expand All @@ -96,7 +107,7 @@ if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
HTTPResumableUploadHandler(
context: uploadContext,
handlers: [
UploadServerHandler(directory: URL(filePath: CommandLine.arguments[1], directoryHint: .isDirectory))
UploadServerHandler(directory: URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true))
]
),
]).flatMap { _ in
Expand Down

0 comments on commit a85ca15

Please sign in to comment.