wip
This commit is contained in:
parent
0a57e0648f
commit
18083e0b19
|
@ -110,31 +110,25 @@ extension Client {
|
||||||
|
|
||||||
func uploadFile(_ localURL: URL) async throws -> String {
|
func uploadFile(_ localURL: URL) async throws -> String {
|
||||||
// get data from file
|
// get data from file
|
||||||
guard let data = try? Data(contentsOf: localURL) else {
|
guard var data = try? Data(contentsOf: localURL) else {
|
||||||
throw AppError.noData
|
throw AppError.noData
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode data with AES_GSM
|
// encrypt data if needed
|
||||||
guard let iv = try? AESGSMEngine.generateIV(), let key = try? AESGSMEngine.generateKey() else {
|
let ((key, iv), encrypted) = try await encryptFile(data)
|
||||||
throw AppError.securityError
|
data = encrypted
|
||||||
}
|
|
||||||
var encodedData = Data()
|
|
||||||
var tag = Data()
|
|
||||||
guard AESGSMEngine.shared.encrypt(iv: iv, key: key, message: data, output: &encodedData, tag: &tag) else {
|
|
||||||
throw AppError.securityError
|
|
||||||
}
|
|
||||||
|
|
||||||
// upload
|
// upload
|
||||||
let httpModule = connection.module(HttpFileUploadModule.self)
|
let httpModule = connection.module(HttpFileUploadModule.self)
|
||||||
let components = try await httpModule.findHttpUploadComponents()
|
let components = try await httpModule.findHttpUploadComponents()
|
||||||
guard let component = components.first(where: { $0.maxSize > encodedData.count }) else {
|
guard let component = components.first(where: { $0.maxSize > data.count }) else {
|
||||||
throw AppError.fileTooBig
|
throw AppError.fileTooBig
|
||||||
}
|
}
|
||||||
|
|
||||||
let slot = try await httpModule.requestUploadSlot(
|
let slot = try await httpModule.requestUploadSlot(
|
||||||
componentJid: component.jid,
|
componentJid: component.jid,
|
||||||
filename: localURL.lastPathComponent,
|
filename: localURL.lastPathComponent,
|
||||||
size: encodedData.count,
|
size: data.count,
|
||||||
contentType: localURL.mimeType
|
contentType: localURL.mimeType
|
||||||
)
|
)
|
||||||
var request = URLRequest(url: slot.putUri)
|
var request = URLRequest(url: slot.putUri)
|
||||||
|
@ -142,8 +136,8 @@ extension Client {
|
||||||
request.addValue(value, forHTTPHeaderField: key)
|
request.addValue(value, forHTTPHeaderField: key)
|
||||||
}
|
}
|
||||||
request.httpMethod = "PUT"
|
request.httpMethod = "PUT"
|
||||||
request.httpBody = encodedData
|
request.httpBody = data
|
||||||
request.addValue(String(encodedData.count), forHTTPHeaderField: "Content-Length")
|
request.addValue(String(data.count), forHTTPHeaderField: "Content-Length")
|
||||||
request.addValue(localURL.mimeType, forHTTPHeaderField: "Content-Type")
|
request.addValue(localURL.mimeType, forHTTPHeaderField: "Content-Type")
|
||||||
let (_, response) = try await URLSession.shared.data(for: request)
|
let (_, response) = try await URLSession.shared.data(for: request)
|
||||||
switch response {
|
switch response {
|
||||||
|
@ -198,6 +192,19 @@ private extension Client {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encryptFile(_ data: Data) async throws -> ((Data, Data), Data) {
|
||||||
|
guard let iv = try? AESGSMEngine.generateIV(), let key = try? AESGSMEngine.generateKey() else {
|
||||||
|
throw AppError.securityError
|
||||||
|
}
|
||||||
|
var encrypted = Data()
|
||||||
|
var tag = Data()
|
||||||
|
guard AESGSMEngine.shared.encrypt(iv: iv, key: key, message: data, output: &encrypted, tag: &tag) else {
|
||||||
|
throw AppError.securityError
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((key, iv), encrypted)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Client {
|
extension Client {
|
||||||
|
|
Loading…
Reference in a new issue