UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

96 lines (95 loc) 3.83 kB
/** * DevExtreme (esm/__internal/ui/file_uploader/file_upload_strategy.chunks.base.js) * Version: 25.2.5 * Build date: Fri Feb 20 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import Guid from "../../../core/guid"; import { Deferred } from "../../../core/utils/deferred"; import { FileBlobReader } from "../../ui/file_uploader/file_blob_reader"; import { FileUploadStrategyBase } from "../../ui/file_uploader/file_upload_strategy.base"; export class ChunksFileUploadStrategyBase extends FileUploadStrategyBase { constructor(fileUploader) { super(fileUploader); const { chunkSize: chunkSize } = this.fileUploader.option(); this.chunkSize = chunkSize ?? 0 } _uploadCore(file) { const realFile = file.value; const chunksData = { name: realFile.name, loadedBytes: 0, type: realFile.type, blobReader: new FileBlobReader(realFile, this.chunkSize), guid: new Guid, fileSize: realFile.size, count: this._getFileChunksCount(realFile), customData: {} }; file.chunksData = chunksData; this._sendChunk(file, chunksData) } _getFileChunksCount(jsFile) { return 0 === jsFile.size ? 1 : Math.ceil(jsFile.size / this.chunkSize) } _sendChunk(file, chunksData) { const chunk = chunksData.blobReader.read(); chunksData.currentChunk = chunk; if (chunk) { this._sendChunkCore(file, chunksData, chunk).done((() => { var _chunk$blob; if (file.isAborted) { return } chunksData.loadedBytes += (null === (_chunk$blob = chunk.blob) || void 0 === _chunk$blob ? void 0 : _chunk$blob.size) ?? 0; file.onProgress.fire({ loaded: chunksData.loadedBytes, total: file.value.size }); if (chunk.isCompleted) { file.onLoad.fire() } setTimeout((() => this._sendChunk(file, chunksData))) })).fail((error => { if (this._shouldHandleError(file, error)) { this._handleFileError(file, error) } })) } } _sendChunkCore(_file, _chunksData, _chunk) { return Deferred().reject() } _tryRaiseStartLoad(file) { if (!file.isStartLoad) { file.isStartLoad = true; file.onLoadStart.fire() } } _getEvent(_e) { return null } _createUploadArgument(file) { return this._createChunksInfo(file.chunksData) } _createChunksInfo(chunksData) { var _chunksData$currentCh, _chunksData$currentCh2; return { bytesUploaded: (null === chunksData || void 0 === chunksData ? void 0 : chunksData.loadedBytes) ?? 0, chunkCount: (null === chunksData || void 0 === chunksData ? void 0 : chunksData.count) ?? 0, customData: (null === chunksData || void 0 === chunksData ? void 0 : chunksData.customData) ?? {}, chunkBlob: (null === chunksData || void 0 === chunksData || null === (_chunksData$currentCh = chunksData.currentChunk) || void 0 === _chunksData$currentCh ? void 0 : _chunksData$currentCh.blob) ?? new Blob, chunkIndex: (null === chunksData || void 0 === chunksData || null === (_chunksData$currentCh2 = chunksData.currentChunk) || void 0 === _chunksData$currentCh2 ? void 0 : _chunksData$currentCh2.index) ?? 0 } } }