song.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { Observable } from 'rxjs';
  4. import { Song } from '../models/song';
  5. import { parseString } from 'xml2js';
  6. import { Riff } from '../models/riff';
  7. @Injectable({
  8. providedIn: 'root'
  9. })
  10. export class SongService {
  11. apiUrl = 'http://localhost:8080/song';
  12. private pdfUrl = 'http://localhost:8080/pdf';
  13. constructor(private http: HttpClient) {}
  14. getAllSongs(): Observable<Song[]> {
  15. return this.http.get<Song[]>(this.apiUrl);
  16. }
  17. uploadSong(song: Song): Observable<Song> {
  18. const headers = new HttpHeaders({
  19. 'Content-Type': 'application/json'
  20. });
  21. return this.http.post<Song>(this.apiUrl, song, { headers });
  22. }
  23. parseXmlToSong(xmlContent: string): Observable<Song> {
  24. return new Observable<Song>((observer) => {
  25. parseString(xmlContent, (err, result) => {
  26. if (err) {
  27. observer.error(err);
  28. }
  29. else {
  30. const track = result.Track;
  31. const name = track.Name[0];
  32. const riffsXml = track.Riffs[0].Riff;
  33. const riffs = riffsXml.map((riffXml: any) => {
  34. return new Riff(
  35. 0,
  36. riffXml.Name[0],
  37. riffXml.StartMs[0],
  38. riffXml.EndMs[0],
  39. riffXml.GroupColor[0]
  40. );
  41. });
  42. const song = new Song(0, name, '', riffs, 0, 0, 0, new Date(), new Date());
  43. observer.next(song);
  44. observer.complete();
  45. }
  46. });
  47. });
  48. }
  49. generateXaml(song: Song): string {
  50. const songXml = this.songToXml(song);
  51. const xaml = `<Song>${songXml}</Song>`;
  52. return xaml;
  53. }
  54. songToXml(song: Song): string {
  55. const xmlBuilder = require('xmlbuilder');
  56. const songXml = xmlBuilder.create('Track', { version: '1.0', encoding: 'UTF-8' });
  57. if (song.riffs && song.riffs.length > 0) {
  58. const riffsXml = songXml.ele('Riffs');
  59. for (const riff of song.riffs) {
  60. const riffXml = riffsXml.ele('Riff');
  61. riffXml.ele('Name', riff.name);
  62. riffXml.ele('StartMs', riff.startMs);
  63. riffXml.ele('EndMs', riff.endMs);
  64. riffXml.ele('GroupColor', riff.groupColor);
  65. riffXml.up();
  66. }
  67. }
  68. songXml.ele('Name', song.name);
  69. return songXml.end({ pretty: true });
  70. }
  71. // generatePdf(songId: number): void {
  72. // this.http.get(`${this.pdfUrl}/${songId}`, { responseType: 'blob' }).subscribe(
  73. // (response: Blob) => {
  74. // const filename = 'song.pdf'; // Provide a default filename
  75. // const blob = new Blob([response], { type: 'application/pdf' });
  76. // this.downloadFile(blob, filename);
  77. // },
  78. // (error: any) => {
  79. // console.error('Error generating PDF:', error);
  80. // // Handle error if needed
  81. // }
  82. // );
  83. // }
  84. //
  85. //
  86. // downloadFile(blob: Blob, filename: string): void {
  87. // const anchorElement = document.createElement('a');
  88. // anchorElement.href = window.URL.createObjectURL(blob);
  89. // anchorElement.download = filename;
  90. // anchorElement.click();
  91. // window.URL.revokeObjectURL(anchorElement.href);
  92. // }
  93. generatePdf(songId: number): Observable<Blob> {
  94. return this.http.get(`${this.pdfUrl}/${songId}`, { responseType: 'blob' });
  95. }
  96. }