Skip to content

Commit

Permalink
Prevent issues with long file names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnpn committed Apr 26, 2024
1 parent 3939e92 commit 49e7767
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Azayaka.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 49;
CURRENT_PROJECT_VERSION = 50;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -352,7 +352,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 49;
CURRENT_PROJECT_VERSION = 50;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
16 changes: 15 additions & 1 deletion Azayaka/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,22 @@ struct Preferences: View {
struct OutputSettings: View {
@AppStorage("saveDirectory") private var saveDirectory: String?
@AppStorage(fileName) private var _fileName: String = "Recording at %t"
@State private var fileNameLength = 0
private let dateFormatter = DateFormatter()

var body: some View {
VStack() {
GroupBox() {
VStack() {
TextField("File name", text: $_fileName).frame(maxWidth: 250)
.onChange(of: _fileName) { newText in
fileNameLength = getFileNameLength(newText)
}
.onAppear() {
dateFormatter.dateFormat = "y-MM-dd HH.mm.ss"
fileNameLength = getFileNameLength(_fileName)
}
.foregroundStyle(fileNameLength > NAME_MAX ? .red : .primary)
Text("\"%t\" will be replaced with the recording's start time.")
.font(.subheadline).foregroundColor(Color.gray)
}.padding(10).frame(maxWidth: .infinity)
Expand All @@ -191,7 +201,11 @@ struct Preferences: View {
}
}
}


func getFileNameLength(_ fileName: String) -> Int {
return fileName.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date())).count
}

func updateOutputDirectory() { // todo: re-sandbox?
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
Expand Down
6 changes: 4 additions & 2 deletions Azayaka/Recording.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension AppDelegate {
case AudioFormat.alac.rawValue: fileEnding = "m4a"
case AudioFormat.flac.rawValue: fileEnding = "flac"
case AudioFormat.opus.rawValue: fileEnding = "ogg"
default: assertionFailure("loaded unknown audio format: ".local + fileEnding)
default: assertionFailure("loaded unknown audio format: ".local + fileEnding)
}
filePath = "\(getFilePath()).\(fileEnding)"
audioFile = try! AVAudioFile(forWriting: URL(fileURLWithPath: filePath), settings: audioSettings, commonFormat: .pcmFormatFloat32, interleaved: false)
Expand All @@ -162,7 +162,9 @@ extension AppDelegate {
if fileName == nil || fileName!.isEmpty {
fileName = "Recording at %t".local
}
return ud.string(forKey: "saveDirectory")! + "/" + fileName!.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date()))
// bit of a magic number but worst case ".flac" is 5 characters on top of this..
let fileNameWithDates = fileName!.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date())).prefix(Int(NAME_MAX) - 5)
return ud.string(forKey: "saveDirectory")! + "/" + fileNameWithDates
}

func getRecordingLength() -> String {
Expand Down

0 comments on commit 49e7767

Please sign in to comment.