0001 // 0002 // Upload.swift 0003 // SwiftHTTP 0004 // 0005 // Created by Dalton Cherry on 6/5/14. 0006 // Copyright (c) 2014 Vluxe. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 #if os(iOS) 0012 import MobileCoreServices 0013 #endif 0014 0015 /** 0016 Upload errors 0017 */ 0018 enum HTTPUploadError: ErrorType { 0019 case NoFileUrl
Upload.swift:58 guard let url = fileUrl else { throw HTTPUploadError.NoFileUrl }0020 } 0021 0022 0023 /** 0024 This is how to upload files in SwiftHTTP. The upload object represents a file to upload by either a data blob or a url (which it reads off disk). 0025 */ 0026 public class Upload
Upload.swift:58 guard let url = fileUrl else { throw HTTPUploadError.NoFileUrl }: NSObject, NSCoding { 0027 var fileUrl
Request.swift:59 return storeVal as? UploadRequest.swift:58 var upload: Upload? {Request.swift:160 extension Upload: HTTPParameterProtocol {: NSURL? { 0028 didSet { 0029 getMimeType() 0030 } 0031 } 0032 var mimeType
Upload.swift:41 guard let url = fileUrl else { return }Upload.swift:58 guard let url = fileUrl else { throw HTTPUploadError.NoFileUrl }Upload.swift:69 aCoder.encodeObject(self.fileUrl, forKey: "fileUrl")Upload.swift:84 fileUrl = aDecoder.decodeObjectForKey("fileUrl") as? NSURLUpload.swift:97 self.fileUrl = fileUrl: String? 0033 var data
Request.swift:277 type: upload.mimeType, multiCRLF: multiCRLF).dataUsingEncoding(NSUTF8StringEncoding)!)Upload.swift:40 mimeType = "application/octet-stream"Upload.swift:46 mimeType = str.takeRetainedValue() as StringUpload.swift:70 aCoder.encodeObject(self.mimeType, forKey: "mimeType")Upload.swift:85 mimeType = aDecoder.decodeObjectForKey("mimeType") as? StringUpload.swift:112 self.mimeType = mimeType: NSData? 0034 var fileName
Upload.swift:55 if let d = data {Upload.swift:61 data = dUpload.swift:72 aCoder.encodeObject(self.data, forKey: "data")Upload.swift:87 data = aDecoder.decodeObjectForKey("data") as? NSDataUpload.swift:110 self.data = data: String? 0035 0036 /** 0037 Tries to determine the mime type from the fileUrl extension. 0038 */ 0039 func getMimeType
Request.swift:276 mutData.appendData(multiFormHeader(key, fileName: upload.fileName,Upload.swift:59 fileName = url.lastPathComponentUpload.swift:71 aCoder.encodeObject(self.fileName, forKey: "fileName")Upload.swift:86 fileName = aDecoder.decodeObjectForKey("fileName") as? StringUpload.swift:111 self.fileName = fileName() { 0040 mimeType = "application/octet-stream" 0041 guard let url = fileUrl else { return } 0042 #if os(iOS) || os(OSX) //for watchOS support 0043 if let ext = url.pathExtension { 0044 guard let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext, nil) else { return } 0045 guard let str = UTTypeCopyPreferredTagWithClass(UTI.takeRetainedValue(), kUTTagClassMIMEType) else { return } 0046 mimeType = str.takeRetainedValue() as String 0047 } 0048 #endif 0049 } 0050 0051 /** 0052 Reads the data from disk or from memory. Throws an error if no data or file is found. 0053 */ 0054 public func getData
Upload.swift:29 getMimeType()() throws -> NSData { 0055 if let d = data { 0056 return d 0057 } 0058 guard let url = fileUrl else { throw HTTPUploadError.NoFileUrl } 0059 fileName = url.lastPathComponent 0060 let d = try NSData(contentsOfURL: url, options: NSDataReadingOptions.DataReadingMappedIfSafe) 0061 data = d 0062 return d 0063 } 0064 0065 /** 0066 Standard NSCoder support 0067 */ 0068 public func encodeWithCoder(aCoder: NSCoder) { 0069 aCoder.encodeObject(self.fileUrl, forKey: "fileUrl") 0070 aCoder.encodeObject(self.mimeType, forKey: "mimeType") 0071 aCoder.encodeObject(self.fileName, forKey: "fileName") 0072 aCoder.encodeObject(self.data, forKey: "data") 0073 } 0074 0075 /** 0076 Required for NSObject support (because of NSCoder, it would be a struct otherwise!) 0077 */ 0078 public override init
Request.swift:275 let data = try upload.getData()() { 0079 super.init() 0080 } 0081 0082 required public convenience init(coder aDecoder: NSCoder) { 0083 self.init() 0084 fileUrl = aDecoder.decodeObjectForKey("fileUrl") as? NSURL 0085 mimeType = aDecoder.decodeObjectForKey("mimeType") as? String 0086 fileName = aDecoder.decodeObjectForKey("fileName") as? String 0087 data = aDecoder.decodeObjectForKey("data") as? NSData 0088 } 0089 0090 /** 0091 Initializes a new Upload object with a fileUrl. The fileName and mimeType will be infered. 0092 0093 -parameter fileUrl: The fileUrl is a standard url path to a file. 0094 */ 0095 public convenience init(fileUrl: NSURL) { 0096 self.init() 0097 self.fileUrl = fileUrl 0098 } 0099 0100 /** 0101 Initializes a new Upload object with a data blob. 0102 0103 -parameter data: The data is a NSData representation of a file's data. 0104 -parameter fileName: The fileName is just that. The file's name. 0105 -parameter mimeType: The mimeType is just that. The mime type you would like the file to uploaded as. 0106 */ 0107 ///upload a file from a a data blob. Must add a filename and mimeType as that can't be infered from the data 0108 public convenience init(data: NSData, fileName: String, mimeType: String) { 0109 self.init() 0110 self.data = data 0111 self.fileName = fileName 0112 self.mimeType = mimeType 0113 } 0114 } 0115
Upload.swift:83 self.init()Upload.swift:96 self.init()Upload.swift:109 self.init()