-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFileUtil.swift
More file actions
76 lines (61 loc) · 2.08 KB
/
FileUtil.swift
File metadata and controls
76 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import UIKit
/**
This class contains utility functions for processing files
*/
public class FileUtil {
public init() {
}
/**
create a file path meant for saving document scan image
@param pageNumber the document page number
@return the file path url
*/
public func createImageFile(_ pageNumber: Int) -> URL {
// create a file path with page number and date time to avoid
// repeating file names
return FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent(
"DOCUMENT_SCAN_\(pageNumber)_\(getCurrentDateTime()).jpg"
)
}
/**
get current date and time
@return current date and time
*/
private func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd_HHmmss"
return dateFormatter.string(from: Date())
}
/**
get file path url from file path string
@param imageFilePath the image file path string
@return the file path url
*/
private func getImageUrl(_ imageFilePath: String) throws -> URL {
guard let imageUrl: URL = NSURL(string: imageFilePath) as URL? else {
throw RuntimeError.message("Unable to get image from file")
}
return imageUrl
}
/**
get image in base64
@param imageFilePath the image file path
@return the image in base64
*/
public func getBase64Image(imageFilePath: String) throws -> String {
guard let imageData: Data = NSData(contentsOf: try getImageUrl(imageFilePath)) as Data? else {
throw RuntimeError.message("Unable to get image from file")
}
return imageData.base64EncodedString()
}
/**
delete image
@param imageFilePath the image file path to delete
*/
public func deleteImage(imageFilePath: String) throws {
try FileManager.default.removeItem(at: getImageUrl(imageFilePath))
}
}