Skip to content

Commit

Permalink
CreamAsset from URL no longer have to be converted to Data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Roy authored and caiyue1993 committed Aug 4, 2020
1 parent 112396b commit 47b3f5f
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions IceCream/Classes/CreamAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import CloudKit
/// So this is the deal.
public class CreamAsset: Object {
@objc dynamic var uniqueFileName = ""
@objc dynamic var data: Data?
@objc dynamic private var data: Data?
override public static func ignoredProperties() -> [String] {
return ["data", "filePath"]
}
Expand All @@ -31,16 +31,33 @@ public class CreamAsset: Object {
save(data: data, to: uniqueFileName, shouldOverwrite: shouldOverwrite)
}

private convenience init?(objectID: String, propName: String, url: URL) {
private convenience init?(objectID: String, propName: String, url: URL, copyFile: Bool) {
guard let data = try? Data(contentsOf: url) else { return nil }
self.init(objectID: objectID, propName: propName, data: data)
}

if copyFile {
self.init()
self.uniqueFileName = "\(objectID)_\(propName).\(url.pathExtension)"

do {
try FileManager.default.copyItem(at: url, to: filePath)
} catch {
print("Error writing asset to temporary directory: \(error)")
}
}
else {
self.init(objectID: objectID, propName: propName, data: data)
}
}

/// There is an important point that we need to consider:
/// Cuz we only store the path of data, so we can't access data by `data` property
/// So use this method if you want get the data of this object
public func storedData() -> Data? {
return try? Data(contentsOf: filePath)
if data == nil {
data = try? Data(contentsOf: filePath)
}

return data
}

public var filePath: URL {
Expand All @@ -55,7 +72,7 @@ public class CreamAsset: Object {
do {
try data.write(to: url)
} catch {
print("Error writing avatar to temporary directory: \(error)")
print("Error writing asset to temporary directory: \(error)")
}
}

Expand All @@ -74,7 +91,7 @@ public class CreamAsset: Object {
/// - Returns: A CreamAsset if it was successful
static func parse(from propName: String, record: CKRecord, asset: CKAsset) -> CreamAsset? {
guard let url = asset.fileURL else { return nil }
return CreamAsset(objectID: record.recordID.recordName, propName: propName, url: url)
return CreamAsset(objectID: record.recordID.recordName, propName: propName, url: url, copyFile: true)
}

/// Creates a new CreamAsset for the given object with Data
Expand Down Expand Up @@ -114,11 +131,12 @@ public class CreamAsset: Object {
/// - propName: The unique property name to identify this asset. e.g.: Dog Object may have multiple CreamAsset properties, so we need unique `propName`s to identify these.
/// - url: The URL of the file to store. Any path extension on the file (e.g. "mov") will be maintained
/// - Returns: A CreamAsset if it was successful
public static func create(object: CKRecordConvertible, propName: String, url: URL) -> CreamAsset? {
public static func create(object: CKRecordConvertible, propName: String, url: URL, copyFile: Bool = false) -> CreamAsset? {

return CreamAsset(objectID: object.recordID.recordName,
propName: propName,
url: url)
url: url,
copyFile: copyFile)
}
}

Expand Down

0 comments on commit 47b3f5f

Please sign in to comment.