2017-02-11 23:45:00 +08:00
|
|
|
//
|
|
|
|
|
// PasswordEntity.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 11/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import SwiftyUserDefaults
|
|
|
|
|
|
|
|
|
|
extension PasswordEntity {
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public var nameWithCategory: String {
|
2018-12-15 21:49:18 +01:00
|
|
|
if let p = path, p.hasSuffix(".gpg") {
|
|
|
|
|
return String(p.prefix(upTo: p.index(p.endIndex, offsetBy: -4)))
|
2017-03-16 00:38:38 +08:00
|
|
|
}
|
2018-12-15 21:49:18 +01:00
|
|
|
return ""
|
2017-03-16 00:38:38 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getCategoryText() -> String {
|
2017-10-28 15:32:30 +08:00
|
|
|
return getCategoryArray().joined(separator: " > ")
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-28 15:32:30 +08:00
|
|
|
public func getCategoryArray() -> [String] {
|
2017-03-10 23:01:56 -08:00
|
|
|
var parentEntity = parent
|
|
|
|
|
var passwordCategoryArray: [String] = []
|
|
|
|
|
while parentEntity != nil {
|
|
|
|
|
passwordCategoryArray.append(parentEntity!.name!)
|
|
|
|
|
parentEntity = parentEntity!.parent
|
|
|
|
|
}
|
|
|
|
|
passwordCategoryArray.reverse()
|
2017-10-28 15:32:30 +08:00
|
|
|
return passwordCategoryArray
|
2017-03-10 23:01:56 -08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-06-09 22:18:54 -07:00
|
|
|
public func getURL() throws -> URL {
|
|
|
|
|
if let p = getPath().stringByAddingPercentEncodingForRFC3986(), let u = URL(string: p) {
|
|
|
|
|
return u
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2019-06-09 22:18:54 -07:00
|
|
|
throw AppError.Unknown
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2018-11-10 22:38:12 -08:00
|
|
|
|
|
|
|
|
// XXX: define some getters to get core data, we need to consider
|
|
|
|
|
// manually write models instead auto generation.
|
|
|
|
|
|
|
|
|
|
public func getImage() -> Data? {
|
|
|
|
|
return image
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func getName() -> String {
|
|
|
|
|
// unwrap non-optional core data
|
|
|
|
|
return name ?? ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func getPath() -> String {
|
|
|
|
|
// unwrap non-optional core data
|
|
|
|
|
return path ?? ""
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-11 23:45:00 +08:00
|
|
|
}
|