react-native-compressor
Version:
Compress Image, Video, and Audio same like Whatsapp & Auto/Manual Compression | Background Upload | Download File | Create Video Thumbnail
56 lines (55 loc) • 1.87 kB
JavaScript
;
import { NativeEventEmitter, Platform } from 'react-native';
import { Compressor } from '../Main';
const CompressEventEmitter = new NativeEventEmitter(Compressor);
import { uuidv4 } from './helpers';
export let UploadType = /*#__PURE__*/function (UploadType) {
UploadType[UploadType["BINARY_CONTENT"] = 0] = "BINARY_CONTENT";
UploadType[UploadType["MULTIPART"] = 1] = "MULTIPART";
return UploadType;
}({});
export let UploaderHttpMethod = /*#__PURE__*/function (UploaderHttpMethod) {
UploaderHttpMethod["POST"] = "POST";
UploaderHttpMethod["PUT"] = "PUT";
UploaderHttpMethod["PATCH"] = "PATCH";
return UploaderHttpMethod;
}({});
export const cancelUpload = (uuid = '', shouldCancelAll = false) => {
return Compressor.cancelUpload(uuid, shouldCancelAll);
};
export const backgroundUpload = async (url, fileUrl, options, onProgress, abortSignal) => {
const uuid = uuidv4();
let subscription;
try {
if (onProgress) {
subscription = CompressEventEmitter.addListener('uploadProgress', event => {
if (event.uuid === uuid) {
onProgress(event.data.written, event.data.total);
}
});
}
if (Platform.OS === 'android' && fileUrl.includes('file://')) {
fileUrl = fileUrl.replace('file://', '');
}
if (options?.getCancellationId) {
options?.getCancellationId(uuid);
}
abortSignal?.addEventListener('abort', () => cancelUpload(uuid));
const result = await Compressor.upload(fileUrl, {
uuid,
method: options.httpMethod,
headers: options.headers,
uploadType: options.uploadType,
...options,
url
});
return result;
} finally {
// @ts-ignore
if (subscription) {
subscription.remove();
}
abortSignal?.removeEventListener('abort', () => cancelUpload(uuid));
}
};
//# sourceMappingURL=Uploader.js.map