aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilyayudovin <ilyayudovin123@gmail.com>2020-08-19 20:02:33 +0300
committerilyayudovin <ilyayudovin123@gmail.com>2020-08-19 20:02:33 +0300
commita42667af463b8c33a38b935b96d39582b543790b (patch)
treeca4bf087fe3b3f60ecf77532b2ae65d28d8af94b /src
parent9afe4cda2153058db75c9e0f5c6275fc820f83cb (diff)
parentd231604a88ea48f3ba7508837d28896ced25b958 (diff)
downloadwhich-ui-a42667af463b8c33a38b935b96d39582b543790b.tar.gz
fix conflitcs
Diffstat (limited to 'src')
-rw-r--r--src/hooks/useS3Preupload.tsx38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/hooks/useS3Preupload.tsx b/src/hooks/useS3Preupload.tsx
index 3834a48..651bfb6 100644
--- a/src/hooks/useS3Preupload.tsx
+++ b/src/hooks/useS3Preupload.tsx
@@ -1,7 +1,8 @@
-/* eslint-disable */
import { useState, useCallback, useMemo } from 'react';
import axios from 'axios';
-import { post } from '../requests';
+import Bluebird from 'bluebird';
+import Compressor from 'compressorjs';
+import { get } from '../requests';
interface ProgressEvent {
@@ -16,6 +17,17 @@ interface Hook {
progress: number;
}
+
+const compressFile = (file: File, quality = 0.6): Promise<Blob> => {
+ return new Promise((resolve, reject) => {
+ return new Compressor(file, {
+ success: result => resolve(result),
+ error: err => reject(err),
+ quality
+ });
+ });
+};
+
export default (): Hook => {
const [url, setUrl] = useState<string>();
const [file, setFile] = useState<File>();
@@ -38,7 +50,7 @@ export default (): Hook => {
setProgress(Math.round((progressEvent.loaded * 95) / progressEvent.total));
}, [setProgress]);
- const resolve = useCallback(async (): Promise<string> => {
+ const resolve = useCallback(async (quality?: number): Promise<string> => {
if (file) {
const config = {
headers: { 'Content-Type': 'image/png' },
@@ -46,25 +58,21 @@ export default (): Hook => {
};
setProgress(0.01);
- return post('/files')
- .then(function(response){
- console.log({'resData': response.data});
- debugger;
- return response.data;
- })
- .then(function(uploadUrl){
- console.log({'uploadUrl': uploadUrl});
- return axios.put(uploadUrl, file, config);
+
+ // Add querystring to avoid caching request in some browsers, see
+ // https://stackoverflow.com/questions/59339561/safari-skipping-xmlhttprequests
+ return Bluebird.all([get(`/files?key=${file.name}`), compressFile(file, quality)])
+ .then(([response, compressedFile]) => {
+ const uploadUrl = response.data;
+ return axios.put(uploadUrl, compressedFile, config);
})
- .then(function(response){
+ .then(response => {
setProgress(100);
const uri = response.config.url;
- console.log({'uri': uri?.slice(0, uri.indexOf('?'))});
return uri ? uri.slice(0, uri.indexOf('?')) : '';
});
}
setProgress(100);
- console.log({'url': url});
return url || '';
}, [file, handleUploadProgress, url]);