This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.ts
195 lines (177 loc) · 4.18 KB
/
file.ts
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
import * as Interface from "../interface";
import Block from "./block";
import { guid } from "./utils";
const rExt = /\.([^.]+)$/;
let uid = 1;
export type QZFileProps = Interface.QZFileProps;
/**
* @description 生成上传所需的文件结构构造器
* @export
* @class QZFile
* @implements {Interface.QZFile}
*/
export default class QZFile implements Interface.QZFile {
/**
* 原始的文件对象
*
* @type {File}
* @memberof QZFile
*/
public file: File;
/**
* batchid
*
* @type {string}
* @memberof QZFile
*/
public batch: string;
/**
* 块大小
*
* @type {number}
* @memberof QZFile
*/
public blockSize: number;
/**
* 片大小
*
* @type {number}
* @memberof QZFile
*/
public chunkSize: number;
/**
* 所有块
*
* @type {Block[]}
* @memberof QZFile
*/
public blocks: Block[];
/**
* 文件名称
*
* @type {string}
* @memberof QZFile
*/
public name: string;
/**
* 文件最终修改时间
*
* @type {number}
* @memberof QZFile
*/
public lastModified: number;
/**
* 扩展名
*
* @type {string}
* @memberof QZFile
*/
public ext: string;
/**
* 文件大小
*
* @type {number}
* @memberof QZFile
*/
public size: number;
/**
* 文件mime类型
*
* @type {string}
* @memberof QZFile
*/
public type: string;
/**
* Creates an instance of QZFile.
* @param {Interface.QZFileProps} {
* file,
* blockSize,
* chunkSize,
* batch,
* }
* @memberof QZFile
*/
public constructor({
file,
blockSize,
chunkSize,
batch,
}: Interface.QZFileProps) {
this.file = file;
this.blockSize = blockSize || 4 * 1024 * 1024;
this.batch = batch || guid();
this.size = file.size;
this.name = file.name || "unknown_" + uid++;
this.lastModified = file.lastModified || new Date().getTime();
this.blocks = [];
this.chunkSize = chunkSize || 1 * 1024 * 1024;
let ext: string = rExt.exec(file.name) ? RegExp.$1.toLowerCase() : "";
if (!ext && file.type) {
ext = /\/(jpg|jpeg|png|gif|bmp)$/i.exec(file.type) ? RegExp.$1.toLowerCase() : "";
if (ext) {
this.name += "." + ext;
}
}
this.ext = ext;
if (!file.type && this.ext && ~"jpg,jpeg,png,gif,bmp".indexOf(this.ext)) {
this.type = "image/" + (this.ext === "jpg" ? "jpeg" : this.ext);
} else {
this.type = file.type || "application/octet-stream";
}
}
/**
* 文件二进制切割
*
* @param {number} start
* @param {number} end
* @return {*} {Blob}
* @memberof QZFile
*/
public slice(start: number, end: number): Blob {
const file = this.file;
const slice = file.slice;
return slice.call(file, start, end);
}
/**
* 获取所有块信息
*
* @return {*} {Block[]}
* @memberof QZFile
*/
public getBlocks(): Block[] {
if (this.blocks.length) {
return this.blocks;
}
let startByte = 0;
const blocks = [];
while (startByte < this.size) {
let endByte = startByte + this.blockSize;
if (endByte > this.size) {
endByte = this.size;
}
blocks.push(new Block(this, startByte, endByte));
startByte += this.blockSize;
}
this.blocks = blocks;
return blocks;
}
/**
* 按索引获取块信息
*
* @param {number} index
* @return {*} {Block}
* @memberof QZFile
*/
public getBlockByIndex(index: number): Block {
return this.getBlocks()[index];
}
/**
* 获取总片数量
*
* @return {*} {number}
* @memberof QZFile
*/
public getChunksSize(): number {
return this.getBlocks().map(block => block.getChunks().length).reduce((a, b) => a + b, 0)
}
}