Projekt

Obecné

Profil

Stáhnout (2.03 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Component, OnInit } from '@angular/core';
2
import {QueryService} from '../../../services/query.service';
3
import {HttpEventType} from '@angular/common/http';
4
import Utils from '../../../../Utils';
5

    
6
@Component({
7
  selector: 'app-upload',
8
  templateUrl: './upload.component.html',
9
  styleUrls: ['./upload.component.css']
10
})
11
export class UploadComponent implements OnInit {
12
  selectedFiles: File[] = null;
13
  resultMessage: string = null;
14
  progress = 0;
15

    
16
  constructor( private queryService: QueryService ) { }
17

    
18
  ngOnInit(): void {
19
  }
20

    
21
  onFilesSelected(event) {
22
    this.selectedFiles = event.target.files;
23
    this.progress = 0;
24
    this.resultMessage = null;
25
  }
26

    
27
  onUpload() {
28
    console.log(this.selectedFiles);
29

    
30
    if (!this.checkFiles(this.selectedFiles)) {
31
      console.log('Files are not valid');
32
      return;
33
    }
34

    
35
    this.queryService.uploadFile(this.selectedFiles).subscribe( event => {
36
      if (event.type === HttpEventType.UploadProgress) {
37
        this.progress = Math.round(event.loaded / event.total * 100);
38
      } else if (event.type === HttpEventType.Response ) {
39
        this.resultMessage = 'Successfully uploaded ' + this.selectedFiles.length + ' files';
40
        this.selectedFiles = null;
41
      }
42
    });
43
  }
44

    
45
  checkFiles(selectedFiles: File[]): boolean {
46
    if (selectedFiles === null) {
47
      this.resultMessage = 'Select files first';
48
      return false;
49
    }
50

    
51
    // check if is even
52
    if (selectedFiles.length % 2 !== 0) {
53
      this.resultMessage = 'Always pair of files JPEG and XML';
54
      return false;
55
    }
56

    
57
    // check if files have same name (name.jpeg, name.xml)
58
    for (const file1 of selectedFiles) {
59
      let count = 0;
60
      const temp1: string = Utils.removeFileSuffix(file1);
61

    
62
      for (const file2 of selectedFiles) {
63
        const temp2: string = Utils.removeFileSuffix(file2);
64

    
65
        if (temp1 === temp2) {
66
          count++;
67
        }
68

    
69
      }
70

    
71
      if (count !== 2) {
72
        this.resultMessage = 'File ' + temp1 + ' missing xml or jpeg pair';
73
        return false;
74
      }
75

    
76
    }
77
    return true;
78
  }
79
}
(4-4/4)