1 //  Copyright © 2019 650 Industries. All rights reserved.
2 
3 import Foundation
4 
5 /**
6  * Data class that represents an individual asset. The filename is autogenerated if not explicitly
7  * set (it should be explicitly set when originating from a database query).
8  */
9 @objc(EXUpdatesAsset)
10 @objcMembers
11 public final class UpdateAsset: NSObject {
12   /**
13    * properties determined by asset source
14    */
15 
16   public var key: String?
17   public let type: String?
18   public var url: URL?
19   public var metadata: [String: Any]?
20   public var mainBundleDir: String? // used for embedded assets
21   public var mainBundleFilename: String? // used for embedded assets
22   public var isLaunchAsset = false
23   public var extraRequestHeaders: [String: Any]?
24   public var expectedHash: String? // base64url-encoded sha-256
25 
26   /**
27    * properties determined at runtime by updates implementation
28    */
29 
30   public var downloadTime: Date?
31   public var contentHash: String? // base64url-encoded sha-256
32   public var headers: [String: Any]?
33 
34   /**
35    * properties determined by updates database
36    */
37 
38   public var assetId = 0
39 
40   public required init(key: String?, type: String?) {
41     self.key = key
42     self.type = type
43   }
44 
45   public lazy var filename: String = {
46     var fileExtension = ""
47     if let type = type {
48       if type.hasPrefix(".") {
49         fileExtension = type
50       } else {
51         fileExtension = "." + type
52       }
53     }
54 
55     guard let key = key else {
56       // create a filename that's unlikely to collide with any other asset
57       return String(format: "asset-%d-%u%@", arguments: [Int(Date().timeIntervalSince1970), arc4random(), fileExtension])
58     }
59 
60     return key + fileExtension
61   }()
62 }
63