Skip to content

Commit

Permalink
fix: remove leading/trailing/consecutive spaces from song metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Bentroen committed Jul 7, 2024
1 parent 6b91ce8 commit a6feb38
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
16 changes: 8 additions & 8 deletions server/src/song/song-upload/song-upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { UserDocument } from '@server/user/entity/user.entity';
import { UserService } from '@server/user/user.service';

import { SongDocument, Song as SongEntity } from '../entity/song.entity';
import { generateSongId } from '../song.util';
import { generateSongId, removeExtraSpaces } from '../song.util';

@Injectable()
export class SongUploadService {
Expand Down Expand Up @@ -109,9 +109,9 @@ export class SongUploadService {
const song = new SongEntity();
song.uploader = await this.validateUploader(user);
song.publicId = publicId;
song.title = body.title;
song.originalAuthor = body.originalAuthor;
song.description = body.description;
song.title = removeExtraSpaces(body.title);
song.originalAuthor = removeExtraSpaces(body.originalAuthor);
song.description = removeExtraSpaces(body.description);
song.category = body.category;
song.allowDownload = true || body.allowDownload; //TODO: implement allowDownload;
song.visibility = body.visibility;
Expand Down Expand Up @@ -295,10 +295,10 @@ export class SongUploadService {
// Update NBS file with form values
injectSongFileMetadata(
nbsSong,
body.title,
user.username,
body.originalAuthor,
body.description,
removeExtraSpaces(body.title),
removeExtraSpaces(user.username),
removeExtraSpaces(body.originalAuthor),
removeExtraSpaces(body.description),
body.customInstruments,
);

Expand Down
7 changes: 4 additions & 3 deletions server/src/song/song.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SongWithUser,
} from './entity/song.entity';
import { SongUploadService } from './song-upload/song-upload.service';
import { removeExtraSpaces } from './song.util';

@Injectable()
export class SongService {
Expand Down Expand Up @@ -155,9 +156,9 @@ export class SongService {
await this.songUploadService.processSongPatch(foundSong, body, user);

// Update song document
foundSong.title = body.title;
foundSong.originalAuthor = body.originalAuthor;
foundSong.description = body.description;
foundSong.title = removeExtraSpaces(body.title);
foundSong.originalAuthor = removeExtraSpaces(body.originalAuthor);
foundSong.description = removeExtraSpaces(body.description);
foundSong.category = body.category;
foundSong.allowDownload = body.allowDownload;
foundSong.visibility = body.visibility;
Expand Down
4 changes: 4 additions & 0 deletions server/src/song/song.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { customAlphabet } from 'nanoid';

export function removeExtraSpaces(input: string): string {
return input.replace(/\s+/g, ' ').trim();
}

const alphabet =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

Expand Down

0 comments on commit a6feb38

Please sign in to comment.