1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
3
|
import {BasicService} from './basic.service';
|
4
|
import {catchError} from 'rxjs/operators';
|
5
|
import {Languages} from '../../enums/common.enum';
|
6
|
import {MatSnackBar} from '@angular/material';
|
7
|
import {TranslateService} from '@ngx-translate/core';
|
8
|
import { Config } from '../util/config.service';
|
9
|
|
10
|
@Injectable({
|
11
|
providedIn: 'root'
|
12
|
})
|
13
|
export class FileService extends BasicService {
|
14
|
|
15
|
constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
|
16
|
super(config, http, snackBar, translateService);
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Uploads xlsx file which will be parsed on the server
|
21
|
* @param file xlsx file to be parsed
|
22
|
*/
|
23
|
uploadXlsFile(file: FileList) {
|
24
|
return this.makeImportXlsxAPiCall(file, null);
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* Uploads xlsx file which will be parsed on the server
|
29
|
* @param language specify error message language
|
30
|
* @param file xlsx file to be parsed
|
31
|
*/
|
32
|
uploadXlsFileWithLanguage(file: FileList, language: Languages) {
|
33
|
return this.makeImportXlsxAPiCall(file, language);
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Exports a pdf file from the imported xlsx file
|
38
|
*/
|
39
|
getExportedPdf() {
|
40
|
return this.makeExportPdfApiCall(null);
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Exports a pdf file from the imported xlsx file
|
45
|
* @param language specify error message language
|
46
|
*/
|
47
|
getExportedPdfWithLanguage(language: Languages) {
|
48
|
return this.makeExportPdfApiCall(language);
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Testovaci endpoint pro export PDF souboru
|
53
|
* GET /export/pdf? [lang=<CZ,EN>]
|
54
|
* @param language specify error message language
|
55
|
*/
|
56
|
private makeExportPdfApiCall(language: Languages) {
|
57
|
const httpParams: HttpParams = this.createParams({lang: language});
|
58
|
|
59
|
return this.http.get(this.baseUrl + '/api/export/pdf', {responseType: 'blob', params: httpParams})
|
60
|
.pipe(
|
61
|
catchError(err => this.handleError(err))
|
62
|
);
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Testovaci endpoint pro import XLS souboru
|
67
|
* POST /import/xls?[lang=<CZ,EN>]&file=<binarni_soubor>
|
68
|
* @param file xlsx file to import and parse
|
69
|
* @param language specify error message langauge
|
70
|
*/
|
71
|
private makeImportXlsxAPiCall(file: FileList, language: Languages) {
|
72
|
const fileCount: number = file.length;
|
73
|
const formData = new FormData();
|
74
|
|
75
|
const httpParams: HttpParams = this.createParams({lang: language});
|
76
|
const options = {params: httpParams};
|
77
|
|
78
|
if (fileCount > 0) {
|
79
|
|
80
|
formData.append('file', file.item(0));
|
81
|
return this.http.post(this.baseUrl + '/api/import/xls', formData, options);
|
82
|
}
|
83
|
}
|
84
|
}
|