-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from kimdv/kimdv/graphql-file-upload
File upload concept - Taken from #116
- Loading branch information
Showing
10 changed files
with
394 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule SQLite.swift
updated
11 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Foundation | ||
|
||
public struct GraphQLFile { | ||
let fieldName: String | ||
let originalName: String | ||
let mimeType: String | ||
let inputStream: InputStream | ||
let contentLength: UInt64 | ||
|
||
public init(fieldName: String, originalName: String, mimeType: String = "application/octet-stream", data: Data) { | ||
self.init(fieldName: fieldName, originalName: originalName, mimeType: mimeType, inputStream: InputStream(data: data), contentLength: UInt64(data.count)) | ||
} | ||
|
||
public init?(fieldName: String, originalName: String, mimeType: String = "application/octet-stream", fileURL: URL) { | ||
guard let inputStream = InputStream(url: fileURL) else { | ||
return nil | ||
} | ||
|
||
guard let contentLength = GraphQLFile.getFileSize(fileURL: fileURL) else { | ||
return nil | ||
} | ||
|
||
self.init(fieldName: fieldName, originalName: originalName, mimeType: mimeType, inputStream: inputStream, contentLength: contentLength) | ||
} | ||
|
||
public init(fieldName: String, originalName: String, mimeType: String = "application/octet-stream", inputStream: InputStream, contentLength: UInt64) { | ||
self.fieldName = fieldName | ||
self.originalName = originalName | ||
self.mimeType = mimeType | ||
|
||
self.inputStream = inputStream | ||
self.contentLength = contentLength | ||
} | ||
|
||
private static func getFileSize(fileURL: URL) -> UInt64? { | ||
guard let fileSizeAttribute = try? FileManager.default.attributesOfItem(atPath: fileURL.path)[.size], | ||
let fileSize = fileSizeAttribute as? NSNumber else { | ||
return nil | ||
} | ||
|
||
return fileSize.uint64Value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.