Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue where a voice message disappears after being sent #7351

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
audioPlayer?.stop()
audioRecorder?.stopRecording()

sendRecordingAtURL(temporaryFileURL)
// As we only use a single temporary file, we have to rename it, otherwise it will be deleted once the file is sent and if another recording has been started meanwhile, it will fail.
if let finalFileURL = finalizeRecordingAtURL(temporaryFileURL) {
sendRecordingAtURL(finalFileURL)
}

isInLockedMode = false
updateUI()
Expand Down Expand Up @@ -260,8 +263,8 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
audioRecorder?.stopRecording()

guard isInLockedMode else {
if recordDuration ?? 0 >= Constants.minimumRecordingDuration {
sendRecordingAtURL(temporaryFileURL)
if recordDuration ?? 0 >= Constants.minimumRecordingDuration, let finalRecordingURL = finalizeRecordingAtURL(temporaryFileURL) {
sendRecordingAtURL(finalRecordingURL)
} else {
cancelRecording()
}
Expand Down Expand Up @@ -371,6 +374,23 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
}
}

private func finalizeRecordingAtURL(_ url: URL?) -> URL? {
guard let url = url, FileManager.default.fileExists(atPath: url.path) else {
return nil
}

// We rename the file to something unique, so that we can start a new recording without having to wait for this record to be sent.
let newPath = url.deletingPathExtension().path + "-\(UUID().uuidString)"
let destinationUrl = URL(fileURLWithPath: newPath).appendingPathExtension(url.pathExtension)
do {
try FileManager.default.moveItem(at: url, to: destinationUrl)
} catch {
MXLog.error("[VoiceMessageController] finalizeRecordingAtURL:", context: error)
return nil
}
return destinationUrl
}

private func deleteRecordingAtURL(_ url: URL?) {
// Fix: use url.path instead of url.absoluteString when using FileManager otherwise the url seems to be percent encoded and the file is not found.
guard let url = url, FileManager.default.fileExists(atPath: url.path) else {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/7326.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue where a voice message disappears after being sent.