Skip to content

Commit 49e7767

Browse files
committed
Prevent issues with long file names
1 parent 3939e92 commit 49e7767

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Azayaka.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
319319
CODE_SIGN_STYLE = Automatic;
320320
COMBINE_HIDPI_IMAGES = YES;
321-
CURRENT_PROJECT_VERSION = 49;
321+
CURRENT_PROJECT_VERSION = 50;
322322
DEAD_CODE_STRIPPING = YES;
323323
DEVELOPMENT_TEAM = "";
324324
ENABLE_HARDENED_RUNTIME = YES;
@@ -352,7 +352,7 @@
352352
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
353353
CODE_SIGN_STYLE = Automatic;
354354
COMBINE_HIDPI_IMAGES = YES;
355-
CURRENT_PROJECT_VERSION = 49;
355+
CURRENT_PROJECT_VERSION = 50;
356356
DEAD_CODE_STRIPPING = YES;
357357
DEVELOPMENT_TEAM = "";
358358
ENABLE_HARDENED_RUNTIME = YES;

Azayaka/Preferences.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,22 @@ struct Preferences: View {
169169
struct OutputSettings: View {
170170
@AppStorage("saveDirectory") private var saveDirectory: String?
171171
@AppStorage(fileName) private var _fileName: String = "Recording at %t"
172+
@State private var fileNameLength = 0
173+
private let dateFormatter = DateFormatter()
172174

173175
var body: some View {
174176
VStack() {
175177
GroupBox() {
176178
VStack() {
177179
TextField("File name", text: $_fileName).frame(maxWidth: 250)
180+
.onChange(of: _fileName) { newText in
181+
fileNameLength = getFileNameLength(newText)
182+
}
183+
.onAppear() {
184+
dateFormatter.dateFormat = "y-MM-dd HH.mm.ss"
185+
fileNameLength = getFileNameLength(_fileName)
186+
}
187+
.foregroundStyle(fileNameLength > NAME_MAX ? .red : .primary)
178188
Text("\"%t\" will be replaced with the recording's start time.")
179189
.font(.subheadline).foregroundColor(Color.gray)
180190
}.padding(10).frame(maxWidth: .infinity)
@@ -191,7 +201,11 @@ struct Preferences: View {
191201
}
192202
}
193203
}
194-
204+
205+
func getFileNameLength(_ fileName: String) -> Int {
206+
return fileName.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date())).count
207+
}
208+
195209
func updateOutputDirectory() { // todo: re-sandbox?
196210
let openPanel = NSOpenPanel()
197211
openPanel.canChooseFiles = false

Azayaka/Recording.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extension AppDelegate {
149149
case AudioFormat.alac.rawValue: fileEnding = "m4a"
150150
case AudioFormat.flac.rawValue: fileEnding = "flac"
151151
case AudioFormat.opus.rawValue: fileEnding = "ogg"
152-
default: assertionFailure("loaded unknown audio format: ".local + fileEnding)
152+
default: assertionFailure("loaded unknown audio format: ".local + fileEnding)
153153
}
154154
filePath = "\(getFilePath()).\(fileEnding)"
155155
audioFile = try! AVAudioFile(forWriting: URL(fileURLWithPath: filePath), settings: audioSettings, commonFormat: .pcmFormatFloat32, interleaved: false)
@@ -162,7 +162,9 @@ extension AppDelegate {
162162
if fileName == nil || fileName!.isEmpty {
163163
fileName = "Recording at %t".local
164164
}
165-
return ud.string(forKey: "saveDirectory")! + "/" + fileName!.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date()))
165+
// bit of a magic number but worst case ".flac" is 5 characters on top of this..
166+
let fileNameWithDates = fileName!.replacingOccurrences(of: "%t", with: dateFormatter.string(from: Date())).prefix(Int(NAME_MAX) - 5)
167+
return ud.string(forKey: "saveDirectory")! + "/" + fileNameWithDates
166168
}
167169

168170
func getRecordingLength() -> String {

0 commit comments

Comments
 (0)