All files / file-uploader si-file-dropzone.component.ts

96.15% Statements 100/104
85.18% Branches 23/27
91.3% Functions 21/23
96% Lines 96/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288                                                        1x                 44x 44x                   44x                 44x 44x                   44x 44x                   44x 44x                   44x     44x             44x       44x             44x       44x             44x   44x 6x 6x     44x   44x   44x 44x 44x     42x 42x 1x   41x   42x       1x 1x 1x                       41x       41x     41x 55x   41x   41x 41x             48x       57x               57x 57x 8x 8x 49x 2x 2x   57x       49x 49x       57x 57x 33x   24x       24x   27x 27x     27x 1x   26x         63x 63x 1x 1x   63x 1x 1x 62x 16x 16x   63x       1x 1x   1x 3x 2x 2x 2x 1x 1x     1x 1x 1x 1x 1x 1x   1x 1x 1x               1x 2x 2x 2x 2x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  inject,
  input,
  LOCALE_ID,
  output,
  viewChild
} from '@angular/core';
import { addIcons, elementUpload, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
import { UploadFile } from './si-file-uploader.model';
 
@Component({
  selector: 'si-file-dropzone',
  imports: [SiIconComponent, SiTranslatePipe],
  templateUrl: './si-file-dropzone.component.html',
  styleUrl: './si-file-dropzone.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiFileDropzoneComponent {
  /**
   * Text or translation key of the input file selector (is combined with the `uploadTextRest`).
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.FILE_SELECT:click to upload`)
   * ```
   */
  readonly uploadTextFileSelect = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.FILE_SELECT:click to upload`)
  );
  /**
   * Text or translation key of the drag&drop field (is combined with the `uploadTextFileSelect`).
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.DROP:Drop files here or`)
   * ```
   */
  readonly uploadDropText = input(t(() => $localize`:@@SI_FILE_UPLOADER.DROP:Drop files here or`));
  /**
   * Text or translation key for max file size.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.MAX_SIZE:Maximum upload size`)
   * ```
   */
  readonly maxFileSizeText = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.MAX_SIZE:Maximum upload size`)
  );
  /**
   * Text or translation key for accepted types.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ACCEPTED_FILE_TYPES:Accepted file types`)
   * ```
   */
  readonly acceptText = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.ACCEPTED_FILE_TYPES:Accepted file types`)
  );
  /**
   * Text or translation key of message title if incorrect file type is dragged / dropped.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_TYPE:Incorrect file type selected`)
   * ```
   */
  readonly errorTextFileType = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_TYPE:Incorrect file type selected`)
  );
  /**
   * Message or translation key if file exceeds the maximum file size limit.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_SIZE_EXCEEDED:File exceeds allowed maximum size`)
   * ```
   */
  readonly errorTextFileMaxSize = input(
    t(
      () =>
        $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_SIZE_EXCEEDED:File exceeds allowed maximum size`
    )
  );
  /**
   * Define which file types are suggested in file browser.
   * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#attr-accept
   */
  readonly accept = input<string>();
  /**
   * Define maximal allowed file size in bytes.
   */
  readonly maxFileSize = input<number>();
  /**
   * Defines whether the file input allows selecting multiple files.
   * When {@link directoryUpload} is enabled, this will have no effect.
   *
   * @defaultValue false
   */
  readonly multiple = input(false, { transform: booleanAttribute });
  /**
   * Event emitted when files are added.
   */
  readonly filesAdded = output<UploadFile[]>();
 
  /**
   * Enable directory upload.
   *
   * @defaultValue false
   */
  readonly directoryUpload = input(false, { transform: booleanAttribute });
 
  protected readonly maxFileSizeString = computed(() => {
    const maxFileSize = this.maxFileSize();
    return maxFileSize ? this.fileSizeToString(maxFileSize) : '';
  });
 
  protected readonly icons = addIcons({ elementUpload });
 
  protected dragOver = false;
 
  private readonly fileInput = viewChild.required<ElementRef>('fileInput');
  private locale = inject(LOCALE_ID).toString();
  private numberFormat = new Intl.NumberFormat(this.locale, { maximumFractionDigits: 2 });
 
  protected dropHandler(event: DragEvent): void {
    event.preventDefault();
    if (this.directoryUpload()) {
      this.handleItems(event.dataTransfer!.items);
    } else {
      this.handleFiles(event.dataTransfer!.files);
    }
    this.dragOver = false;
  }
 
  protected dragOverHandler(event: DragEvent): void {
    event.preventDefault();
    event.stopPropagation();
    this.dragOver = true;
  }
 
  protected inputEnterHandler(): void {
    this.fileInput().nativeElement.click();
  }
 
  protected inputHandler(event: Event): void {
    this.handleFiles((event.target as HTMLInputElement).files);
  }
 
  protected handleFiles(files: FileList | null): void {
    Iif (!files?.length) {
      return;
    }
 
    const newFiles: UploadFile[] = [];
 
    // eslint-disable-next-line @typescript-eslint/prefer-for-of
    for (let i = 0; i < files.length; i++) {
      newFiles.push(this.makeUploadFile(files[i]));
    }
    newFiles.sort((a, b) => a.fileName.localeCompare(b.fileName));
 
    this.filesAdded.emit(newFiles);
    this.reset();
  }
 
  /**
   * Reset all the files inside the native file input (and therefore the dropzone).
   */
  reset(): void {
    this.fileInput().nativeElement.value = '';
  }
 
  private makeUploadFile(file: File): UploadFile {
    const uploadFile: UploadFile = {
      fileName: file.name,
      file,
      size: this.fileSizeToString(file.size),
      progress: 0,
      status: 'added'
    };
    // use MIME type of file if set. Otherwise fall back to file name ending
    const ext = '.' + uploadFile.file.name.split('.').pop();
    if (!this.verifyFileType(uploadFile.file.type, ext)) {
      uploadFile.status = 'invalid';
      uploadFile.errorText = this.errorTextFileType();
    } else if (!this.verifyFileSize(uploadFile.file.size)) {
      uploadFile.status = 'invalid';
      uploadFile.errorText = this.errorTextFileMaxSize();
    }
    return uploadFile;
  }
 
  private verifyFileSize(size: number): boolean {
    const maxFileSize = this.maxFileSize();
    return !maxFileSize || size <= maxFileSize;
  }
 
  private verifyFileType(fileType: string | undefined, ext: string | undefined): boolean {
    const accept = this.accept();
    if (!accept) {
      return true;
    }
    Iif (fileType === undefined && ext === undefined) {
      return false;
    }
    // Spec says that comma is the delimiter for filetypes. Also allow pipe for compatibility
    return accept.split(/,|\|/).some(acceptedType => {
      // convert accept glob into regex (example: images/* --> images/.*)
      const acceptedRegexStr = acceptedType.replace('.', '.').replace('*', '.*').trim();
      const acceptedRegex = new RegExp(acceptedRegexStr, 'i');
 
      // if fileType is set and accepted type looks like a MIME type, match that otherwise extension
      if (fileType && acceptedType.includes('/')) {
        return !!fileType.match(acceptedRegex);
      }
      return !!ext?.match(acceptedRegex);
    });
  }
 
  private fileSizeToString(num: number): string {
    let suffix = 'B';
    if (num >= 1_073_741_824) {
      num /= 1_073_741_824;
      suffix = 'GB';
    }
    if (num >= 1_048_576) {
      num /= 1_048_576;
      suffix = 'MB';
    } else if (num >= 1_024) {
      num /= 1_024;
      suffix = 'KB';
    }
    return this.numberFormat.format(num) + suffix;
  }
 
  private handleItems(items: DataTransferItemList): void {
    const newFiles: UploadFile[] = [];
    let pendingEntries = 0;
 
    const traverseFileTree = (item: FileSystemEntry): void => {
      if (item.isFile) {
        (item as FileSystemFileEntry).file(file => {
          newFiles.push(this.makeUploadFile(file));
          if (--pendingEntries === 0) {
            this.filesAdded.emit(newFiles);
            this.reset();
          }
        });
      } else if (item.isDirectory) {
        const dirReader = (item as FileSystemDirectoryEntry).createReader();
        dirReader.readEntries(entries => {
          for (const entry of entries) {
            pendingEntries++;
            traverseFileTree(entry);
          }
          if (--pendingEntries === 0) {
            this.filesAdded.emit(newFiles);
            this.reset();
          }
        });
      }
    };
 
    // items is not an array but of type DataTransferItemList
    // eslint-disable-next-line @typescript-eslint/prefer-for-of
    for (let i = 0; i < items.length; i++) {
      const item = items[i].webkitGetAsEntry();
      if (item) {
        pendingEntries++;
        traverseFileTree(item);
      }
    }
  }
}