Truncate track's title if max length reached

This commit is contained in:
Stefano Brilli 2020-03-31 15:34:14 +02:00
parent 926c8ddc93
commit c7f32df5e6
4 changed files with 38 additions and 4 deletions

View File

@ -59,7 +59,7 @@ export const RenameDialog = (props: {}) => {
event.key === `Enter` && handleDoRename();
}}
onChange={event => {
dispatch(renameDialogActions.setCurrentName(event.target.value));
dispatch(renameDialogActions.setCurrentName(event.target.value.substring(0, 120))); // MAX title length
}}
/>
</DialogContent>

View File

@ -6,8 +6,9 @@ import { actions as errorDialogAction } from './error-dialog-feature';
import { actions as appStateActions } from './app-feature';
import { actions as mainActions } from './main-feature';
import serviceRegistry from '../services/registry';
import { Wireformat } from 'netmd-js';
import { Wireformat, getTracks } from 'netmd-js';
import { AnyAction } from '@reduxjs/toolkit';
import { getAvailableCharsForTrackTitle } from '../utils';
export function pair() {
return async function(dispatch: AppDispatch, getState: () => RootState) {
@ -54,8 +55,13 @@ export function listContent() {
export function renameTrack({ index, newName }: { index: number; newName: string }) {
return async function(dispatch: AppDispatch) {
const { netmdService } = serviceRegistry;
await netmdService!.renameTrack(index, newName);
dispatch(renameDialogActions.setVisible(false));
try {
await netmdService!.renameTrack(index, newName);
} catch (err) {
console.error(err);
dispatch(batchActions([errorDialogAction.setVisible(true), errorDialogAction.setErrorMessage(`Rename failed.`)]));
}
listContent()(dispatch);
};
}
@ -107,7 +113,7 @@ export const WireformatDict: { [k: string]: Wireformat } = {
};
export function convertAndUpload(files: File[], format: string) {
return async function(dispatch: AppDispatch, getState: (state: RootState) => void) {
return async function(dispatch: AppDispatch, getState: () => RootState) {
const { audioExportService, netmdService } = serviceRegistry;
const wireformat = WireformatDict[format];
@ -178,6 +184,10 @@ export function convertAndUpload(files: File[], format: string) {
}
};
let disc = getState().main.disc;
let maxTitleLength = disc ? getAvailableCharsForTrackTitle(getTracks(disc).map(track => track.title || ``)) : -1;
maxTitleLength = Math.floor(maxTitleLength / files.length);
let error: any;
let errorMessage = ``;
let i = 1;
@ -189,6 +199,9 @@ export function convertAndUpload(files: File[], format: string) {
if (extStartIndex > 0) {
title = title.substring(0, extStartIndex);
}
if (maxTitleLength > -1) {
title = title.substring(0, maxTitleLength);
}
trackUpdate.current = i++;
trackUpdate.titleCurrent = title;

View File

@ -4,6 +4,7 @@ import { sleep } from '../utils';
import { assert } from 'netmd-js/dist/utils';
class NetMDMockService implements NetMDService {
public _tracksTitlesMaxLength = 1700;
public _discTitle: string = 'Mock Disc';
public _discCapacity: number = 80 * 60 * 512;
public _tracks: Track[] = [
@ -51,6 +52,10 @@ class NetMDMockService implements NetMDService {
return used;
}
_getTracksTitlesLength() {
return this._tracks.reduce((acc, track) => acc + (track.title?.length ?? 0), 0);
}
async pair() {
return true;
}
@ -89,6 +94,9 @@ class NetMDMockService implements NetMDService {
async finalize() {}
async renameTrack(index: number, newTitle: string) {
if (this._getTracksTitlesLength() + newTitle.length > this._tracksTitlesMaxLength) {
throw new Error(`Track's title too long`);
}
this._tracks[index].title = newTitle;
}
@ -119,6 +127,11 @@ class NetMDMockService implements NetMDService {
progressCallback: (progress: { written: number; encrypted: number; total: number }) => void
) {
progressCallback({ written: 0, encrypted: 0, total: 100 });
if (this._getTracksTitlesLength() + title.length > this._tracksTitlesMaxLength) {
throw new Error(`Track's title too long`);
}
await sleep(0.5);
this._tracks.push({
title,

View File

@ -44,4 +44,12 @@ export function loadPreference<T>(key: string, defaultValue: T): T {
}
}
export function getAvailableCharsForTrackTitle(trackTitles: string[]) {
const maxChars = 1700; // see https://www.minidisc.org/md_toc.html
const usedChars = trackTitles.reduce((acc, title) => {
return acc + title.length;
}, 0);
return maxChars - usedChars;
}
declare let process: any;