All files / coral-component-fileupload/src/scripts FileUploadItem.js

95.31% Statements 61/64
87.32% Branches 62/71
100% Functions 22/22
95.31% Lines 61/64

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 289 290                                                                  2x               2x   2x   2x 2x 2x 2x               2x           140x   140x 140x                   2x         26x   26x   26x         26x 34x   34x     10x 24x 10x 2x 8x 2x 6x 2x     4x   14x   10x     10x 6x 10x     4x   4x   4x       34x                     542x                         40x     4x 2x     4x 2x                         36x     4x                       36x     4x   4x 2x   2x                             36x     4x 4x   4x                             4x                         6x                         6x                         4x                         2x                
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import MIME_TYPES from '../data/mimetypes';
import {transform, validate} from '../../../coral-utils';
 
/**
 Enumeration for {@link FileUploadItem} response types.
 
 @typedef {Object} FileUploadItemResponseTypeEnum
 
 @property {String} TEXT
 String type.
 @property {String} ARRAY_BUFFER
 Array buffer type.
 @property {String} BLOB
 Blob type.
 @property {String} DOCUMENT
 Document type.
 @property {String} JSON
 JavaScript object, parsed from a JSON string returned by the server.
 */
const responseType = {
  TEXT: 'text',
  ARRAY_BUFFER: 'arraybuffer',
  BLOB: 'blob',
  DOCUMENT: 'document',
  JSON: 'json'
};
 
// eg text/plain
const MIME_TYPE_REGEXP = /(.+)\/(.+)$/;
// eg .txt
const FILE_EXTENSION_REGEXP = /\.(.+)$/;
// eg text
const SHORTCUT_REGEXP = /.*/;
const MIME_TYPE_AUDIO = 'audio/*';
const MIME_TYPE_IMAGE = 'image/*';
const MIME_TYPE_VIDEO = 'video/*';
 
/**
 @class Coral.FileUpload.Item
 @classdesc A FileUpload item encapsulating file meta-data
 @param {File|HTMLElement} file
 The file element.
 */
class FileUploadItem {
  /**
   Takes a {File} as argument.
 
   @param {File} file
   */
  constructor(file) {
    this._originalFile = file;
    this._xhr = null;
  }
 
  /**
   The File.
 
   @name file
   @readonly
   @type {File}
   */
  get file() {
    return this._originalFile;
  }
 
  /**
   Array of additional parameters as key:value to be uploaded with the file.
   A parameter must contain a <code>name</code> key:value and optionally a <code>value</code> key:value.
 
   @name parameters
   @type {Array.<Object>}
   @default []
   */I
  get parameters() {
    return this._parameters || [];
  }
 
  set parameters(value) {
    const isValid = Array.isArray(value) && value.every((el) => el && el.name);
 
    if (isValid) {
      this._parameters = value;
    }
  }
 
  /**
   The item xhr <code>withCredentials</code> property.
 
   @name withCredentials
   @type {Boolean}
   @default false
   */
  get withCredentials() {
    return this._withCredentials || false;
  }
 
  set withCredentials(value) {
    this._withCredentials = transform.boolean(value);
  }
 
  /**
   The item xhr <code>timeout</code> property.
 
   @name timeout
   @type {Number}
   @default 0
   */
  get timeout()E {
    return this._timeout || 0;
  }
 
  set timeout(value) {
    const timeout = transform.number(value);
    if (timeout !== null) {
      this._timeout = timeout;
      if (this._xhr) {
        this._xhr.timeout = timeout;
      }
    }
  }
 
  /**
   The item xhr <code>responseType</code> property. See {@link FileUploadItemResponseTypeEnum}.
 
   @name responseType
   @default {FileUploadItemResponseTypeEnum.TEXT}
   @type {String}
   */
  get responseType() {
    return this._responseType || responseType.TEXT;
  }
 
  set responseType(value) {
    value = transform.string(value).toLowerCase();
    this._responseType = validate.enumeration(responseType)(value) && value || responseType.TEXT;
    if (this._xhr) {
      this._xhr.responseType = value;
    }
  }
 
  /**
   The item xhr <code>readyState</code> property.
 
   @name readyState
   @readonly
   @default 0
   @type {Number}
   */
  get readyState() {
    return this._xhr ? this._xhr.readyState : this._readyState || 0;
  }
 
  /**
   The item xhr <code>responseType</code> property. Depends on {@link Coral.FileUpload.Item#responseType}.
 
   @name response
   @readonly
   @default ""
   @type {String|ArrayBuffer|Blob|Document}
   */
  get response() {
    return this._xhr ? this._xhr.response : this._response || '';
  }
 
  /**
   The item xhr <code>responseText</code> property.
 
   @name responseText
   @readonly
   @default ""
   @type {String}
   */
  get responseText() {
    return this._xhr ? this._xhr.responseText : this._responseText || '';
  }
 
  /**
   The item xhr <code>responseXML</code> property.
 
   @name responseXML
   @readonly
   @defaIult null
   @type {HTMLElement}
   */
  get responseXML() {
    return this._xhr ? this._xhr.responseXML : this._responseXML || null;
  }
 
  /**
   The item xhr <code>status</code> property.
 
   @name status
   @readonly
   @default 0
   @type {Number}
   */
  get status() {
    return this._xhr ? this._xhr.status : this._status || 0;
  }
 
  /**
   The item xhr <code>statusText</code> property.
I
   @name statusText
   @readonly
   @default ""
   @type {String}
   */
  get statusText() {
    return this._xhr ? this._xhr.statusText : this._statusText || '';
  }
 
  /** @private */
  _isMimeTypeAllowed(acceptedMimeTypes) {
    let isAllowed = false;
 
    // Unrecognized browser mime types have a file type of ''.
    const fileType = this.file.type || 'application/unknown';
 
    if (!fileType.match(MIME_TYPE_REGEXP)) {
      // File mime type is erroneous
      return false;
    }
 
    return acceptedMimeTypes.split(',').some((allowedMimeType) => {
      allowedMimeType = allowedMimeType.trim();
 
      if (allowedMimeType === '*' ||
        allowedMimeType === '.*' ||
        allowedMimeType === '*/*' ||
        fileType === 'application/unknown') {
        // Explicit wildcard case: allow any file
        // Allow unknown mime types
        isAllowed = true;
      } else if (allowedMimeType.match(MIME_TYPE_REGEXP)) {
        if (allowedMimeType === MIME_TYPE_AUDIO) {
          isAllowed = fileType.indexOf(MIME_TYPE_AUDIO.slice(0, -1)) === 0;
        } else if (allowedMimeType === MIME_TYPE_IMAGE) {
          isAllowed = fileType.indexOf(MIME_TYPE_IMAGE.slice(0, -1)) === 0;
        } else if (allowedMimeType === MIME_TYPE_VIDEO) {
          isAllowed = fileType.indexOf(MIME_TYPE_VIDEO.slice(0, -1)) === 0;
        } else {
          // Proper mime type case: directly compare with file mime type
          isAllowed = fileType === allowedMimeType;
        }
      } else if (allowedMimeType.match(FILE_EXTENSION_REGEXP)) {
        // File extension case
        const allowedMimeTypes = MIME_TYPES[allowedMimeType];
 
        // Depending on OS and browser, a file extension can map to different mime types
        // e.g .csv maps to "text/csv" on Mac OS and to "application/vnd.ms-excel" on Windows
        if (Array.isArray(allowedMimeTypes)) {
          isAllowed = allowedMimeTypes.some((mimeType) => fileType === mimeType);
        } else {
          isAllowed = fileType === MIME_TYPES[allowedMimeType];
        }
      } else if (allowedMimeType.match(SHORTCUT_REGEXP)) {
        // "Shortcut" case: only compare first part of the file mime type with the shortcut
        isAllowed = fileType.split('/')[0] === allowedMimeType;
      }
 
      // Break the loop if file mime type is allowed
      return isAllowed;
    });
  }
 
  /**
   Returns {@link FileUploadItem} response types.
 
   @return {FileUploadItemResponseTypeEnum}
   */
  static get responseType() {
    return responseType;
  }
}
 
export default FileUploadItem;