@@ -5,7 +5,8 @@ public struct GraphQLFile {
55 public let fieldName : String
66 public let originalName : String
77 public let mimeType : String
8- public let inputStream : InputStream
8+ public let data : Data ?
9+ public let fileURL : URL ?
910 public let contentLength : UInt64
1011
1112 /// A convenience constant for declaring your mimetype is octet-stream.
@@ -22,11 +23,12 @@ public struct GraphQLFile {
2223 originalName: String ,
2324 mimeType: String = GraphQLFile . octetStreamMimeType,
2425 data: Data ) {
25- self . init ( fieldName: fieldName,
26- originalName: originalName,
27- mimeType: mimeType,
28- inputStream: InputStream ( data: data) ,
29- contentLength: UInt64 ( data. count) )
26+ self . fieldName = fieldName
27+ self . originalName = originalName
28+ self . mimeType = mimeType
29+ self . data = data
30+ self . fileURL = nil
31+ self . contentLength = UInt64 ( data. count)
3032 }
3133
3234 /// Failable convenience initializer for files in the filesystem
@@ -41,42 +43,30 @@ public struct GraphQLFile {
4143 originalName: String ,
4244 mimeType: String = GraphQLFile . octetStreamMimeType,
4345 fileURL: URL ) {
44- guard let inputStream = InputStream ( url: fileURL) else {
45- return nil
46- }
47-
4846 guard let contentLength = GraphQLFile . getFileSize ( fileURL: fileURL) else {
4947 return nil
5048 }
51-
52- self . init ( fieldName: fieldName,
53- originalName: originalName,
54- mimeType: mimeType,
55- inputStream: inputStream,
56- contentLength: contentLength)
57- }
58-
59- /// Designated Initializer
60- ///
61- /// - Parameters:
62- /// - fieldName: The name of the field this file is being sent for
63- /// - originalName: The original name of the file
64- /// - mimeType: The mime type of the file to send to the server. Defaults to `GraphQLFile.octetStreamMimeType`.
65- /// - inputStream: An input stream to use to acccess data
66- /// - contentLength: The length of the data being sent
67- public init ( fieldName: String ,
68- originalName: String ,
69- mimeType: String = GraphQLFile . octetStreamMimeType,
70- inputStream: InputStream ,
71- contentLength: UInt64 ) {
49+
7250 self . fieldName = fieldName
7351 self . originalName = originalName
7452 self . mimeType = mimeType
75-
76- self . inputStream = inputStream
53+ self . data = nil
54+ self . fileURL = fileURL
7755 self . contentLength = contentLength
7856 }
7957
58+ /// Retrieves the InputStream
59+ ///
60+ public func generateInputStream( ) throws -> InputStream {
61+ if let data = data {
62+ return InputStream ( data: data)
63+ } else if let fileURL = fileURL, let inputStream = InputStream ( url: fileURL) {
64+ return inputStream
65+ }
66+
67+ throw GraphQLError ( " InputStream was not created. " )
68+ }
69+
8070 private static func getFileSize( fileURL: URL ) -> UInt64 ? {
8171 guard let fileSizeAttribute = try ? FileManager . default. attributesOfItem ( atPath: fileURL. path) [ . size] ,
8272 let fileSize = fileSizeAttribute as? NSNumber else {
0 commit comments