import _Promise from "@babel/runtime-corejs2/core-js/promise";

/*
 * @Author: lijianzhang
 * @Date: 2018-10-31 10:53:46
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-12-16 13:08:37
 */
let id = 0;
export default class GdFile {
  constructor(file) {
    this.originFile = file;
    this.status = 'ready';
    this.lastModified = file.lastModified;
    this.lastModifiedDate = file.lastModifiedDate;
    this.id = ++id;
    this.src = '';
    this.name = file.name;
    this.size = file.size;
    this.type = file.type;
    this.percent = 0;
  }

  getImageSize() {
    return new _Promise((resolve, reject) => {
      if (!/image\//.test(this.type)) {
        reject(new Error('该文件不是图像文件'));
      }

      const image = new Image();
      image.src = this.src;

      image.onload = () => {
        resolve({
          width: image.width,
          height: image.height
        });
      };

      image.onerror = error => reject(error);
    });
  }

  toURL() {
    return URL.createObjectURL(this.originFile);
  }

}