@@ -168,11 +168,31 @@ open class EVURLCache: URLCache {
168168 // This works for the game, but not for my site.
169169 return response
170170 } else {
171- EVURLCache . debugLog ( " The file is probably not put in the local path using NSKeyedArchiver \( storagePath) " )
171+ // File was not written using the NSKeyedUnarchiver. Will return the raw file as the cache response
172+ if let content: NSData = NSData ( contentsOfFile: storagePath) {
173+ let mimeType = getMimeType ( storagePath)
174+ let response = URLResponse ( url: url, mimeType: mimeType, expectedContentLength: content. length, textEncodingName: nil )
175+ EVURLCache . debugLog ( " CACHE returning cache response: mimeType = \( mimeType) , path = \( storagePath) " ) ;
176+ return CachedURLResponse ( response: response, data: content as Data )
177+ }
178+ EVURLCache . debugLog ( " CACHE could not be read from \( storagePath) " )
172179 }
173180 return nil
174181 }
175182
183+ // Make sure the correct mimetype is returned from the cache
184+ private static func getMimeType( _ path: String ) -> String {
185+ var mimeType = " text/html "
186+ if let fileExtension: String = path. components ( separatedBy: " . " ) . last {
187+ if let uti: CFString = UTTypeCreatePreferredIdentifierForTag ( kUTTagClassFilenameExtension, fileExtension as NSString , nil ) ? . takeRetainedValue ( ) {
188+ if let type = UTTypeCopyPreferredTagWithClass ( uti, kUTTagClassMIMEType) ? . takeRetainedValue ( ) {
189+ mimeType = type as String
190+ }
191+ }
192+ }
193+ return mimeType
194+ }
195+
176196 // Will be called by NSURLConnection when a request is complete.
177197 open override func storeCachedResponse( _ cachedResponse: CachedURLResponse , for request: URLRequest ) {
178198
0 commit comments