Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f892db55

Přidáno uživatelem Ondřej Váně před téměř 5 roky(ů)

Re #7821: Kontrola uploadu souborů na FE

- přidána metoda pro kontrolu souborů na FE
- vždy musí být pár souborů XML a JPEG se stejným názvem

Zobrazit rozdíly:

fe/fulltextsearch/src/Utils.ts
1
export default class Utils {
2

  
3
  static removeFileSuffix(file: File): string {
4
    return file.name.substring(0, file.name.indexOf('.'));
5
  }
6
}
fe/fulltextsearch/src/app/components/pages/upload/upload.component.html
3 3
    <h1 class="card-header">Upload files</h1>
4 4
    <div class="card-body">
5 5
      <div class="row justify-content-center align-items-center">
6
        <input type="file" (change)="onFilesSelected($event)" multiple #fileInput>
6
        <input type="file" (change)="onFilesSelected($event)" multiple #fileInput accept="image/jpeg, application/xml">
7 7
        <button class="btn btn-outline-secondary" type="button" (click)="fileInput.click()">Pick files</button>
8 8
        <button class="btn btn-outline-secondary" type="button" (click)="onUpload()">Upload</button>
9 9
      </div>
fe/fulltextsearch/src/app/components/pages/upload/upload.component.ts
1 1
import { Component, OnInit } from '@angular/core';
2 2
import {QueryService} from '../../../services/query.service';
3 3
import {HttpEventType} from '@angular/common/http';
4
import Utils from '../../../../Utils';
4 5

  
5 6
@Component({
6 7
  selector: 'app-upload',
......
25 26

  
26 27
  onUpload() {
27 28
    console.log(this.selectedFiles);
28
    if (this.selectedFiles === null) {
29
      this.resultMessage = 'Select files first';
29

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

  
32 35
    this.queryService.uploadFile(this.selectedFiles).subscribe( event => {
33 36
      if (event.type === HttpEventType.UploadProgress) {
34 37
        this.progress = Math.round(event.loaded / event.total * 100);
......
38 41
      }
39 42
    });
40 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
  }
41 79
}

Také k dispozici: Unified diff