From 697279f7fd5d6571a3a7599fb51ba57af2aa8945 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sat, 22 Aug 2020 01:22:51 +0300
Subject: refactor: move compressFile to utils

---
 src/hooks/useS3Preupload.tsx | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

(limited to 'src/hooks/useS3Preupload.tsx')

diff --git a/src/hooks/useS3Preupload.tsx b/src/hooks/useS3Preupload.tsx
index 651bfb6..9516459 100644
--- a/src/hooks/useS3Preupload.tsx
+++ b/src/hooks/useS3Preupload.tsx
@@ -1,9 +1,8 @@
 import { useState, useCallback, useMemo } from 'react';
 import axios from 'axios';
 import Bluebird from 'bluebird';
-import Compressor from 'compressorjs';
 import { get } from '../requests';
-
+import { compressFile } from '../utils/files';
 
 interface ProgressEvent {
   loaded: number;
@@ -17,17 +16,6 @@ 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>();
-- 
cgit v1.2.3


From 668c9f4841e7118b98bb31d8e68640689be99830 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sat, 22 Aug 2020 13:54:56 +0300
Subject: refactor!: simplify file operations

---
 src/hooks/useS3Preupload.tsx | 73 --------------------------------------------
 1 file changed, 73 deletions(-)
 delete mode 100644 src/hooks/useS3Preupload.tsx

(limited to 'src/hooks/useS3Preupload.tsx')

diff --git a/src/hooks/useS3Preupload.tsx b/src/hooks/useS3Preupload.tsx
deleted file mode 100644
index 9516459..0000000
--- a/src/hooks/useS3Preupload.tsx
+++ /dev/null
@@ -1,73 +0,0 @@
-import { useState, useCallback, useMemo } from 'react';
-import axios from 'axios';
-import Bluebird from 'bluebird';
-import { get } from '../requests';
-import { compressFile } from '../utils/files';
-
-interface ProgressEvent {
-  loaded: number;
-  total: number;
-}
-
-interface Hook {
-  setValue: (value: File | string | undefined) => void;
-  isReady: boolean;
-  resolve: () => Promise<string>;
-  progress: number;
-}
-
-export default (): Hook => {
-  const [url, setUrl] = useState<string>();
-  const [file, setFile] = useState<File>();
-  const [progress, setProgress] = useState<number>(0);
-
-  const isReady = useMemo(() => Boolean(file || url), [file, url]);
-
-  const setValue: Hook['setValue'] = useCallback(value => {
-    if (value instanceof File) {
-      setFile(value);
-      setUrl(undefined);
-    } else {
-      setUrl(value);
-      setFile(undefined);
-    }
-  }, [setUrl, setFile]);
-
-  const handleUploadProgress = useCallback((progressEvent: ProgressEvent): void => {
-    // Only allow upload progress reach 95%, and set 100% when request is resolved
-    setProgress(Math.round((progressEvent.loaded * 95) / progressEvent.total));
-  }, [setProgress]);
-
-  const resolve = useCallback(async (quality?: number): Promise<string> => {
-    if (file) {
-      const config = {
-        headers: { 'Content-Type': 'image/png' },
-        onUploadProgress: handleUploadProgress
-      };
-
-      setProgress(0.01);
-
-      // 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(response => {
-          setProgress(100);
-          const uri = response.config.url;
-          return uri ? uri.slice(0, uri.indexOf('?')) : '';
-        });
-    }
-    setProgress(100);
-    return url || '';
-  }, [file, handleUploadProgress, url]);
-
-  return {
-    setValue,
-    isReady,
-    resolve,
-    progress
-  };
-};
-- 
cgit v1.2.3

