upload progress status (#73)

* add upload progress status

* resolve cors issues with proxy returning cors error
This commit is contained in:
Karol Wypchło 2020-03-10 14:15:34 +01:00 committed by GitHub
parent 59c46bd6fc
commit f4760e4481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 11 deletions

View File

@ -22,7 +22,8 @@ server {
# proxy this call to siad endpoint (make sure the ip is correct) # proxy this call to siad endpoint (make sure the ip is correct)
proxy_pass http://127.0.0.1:9980; proxy_pass http://127.0.0.1:9980;
proxy_set_header Expect $http_expect; proxy_set_header Expect $http_expect;
proxy_set_header Access-Control-Allow-Origin: *; add_header Access-Control-Allow-Origin *;
proxy_hide_header Access-Control-Allow-Origin;
# make sure to override user agent header - siad requirement # make sure to override user agent header - siad requirement
proxy_set_header User-Agent: Sia-Agent; proxy_set_header User-Agent: Sia-Agent;
# replace BASE64_AUTHENTICATION with base64 encoded <user>:<password> # replace BASE64_AUTHENTICATION with base64 encoded <user>:<password>

View File

@ -7,6 +7,7 @@ import { Button, UploadFile } from "../";
import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg"; import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg";
import "./HomeUpload.scss"; import "./HomeUpload.scss";
import AppContext from "../../AppContext"; import AppContext from "../../AppContext";
import axios from "axios";
export default function HomeUpload() { export default function HomeUpload() {
const [files, setFiles] = useState([]); const [files, setFiles] = useState([]);
@ -15,7 +16,7 @@ export default function HomeUpload() {
const handleDrop = async (acceptedFiles) => { const handleDrop = async (acceptedFiles) => {
setFiles((previousFiles) => [...acceptedFiles.map((file) => ({ file, status: "uploading" })), ...previousFiles]); setFiles((previousFiles) => [...acceptedFiles.map((file) => ({ file, status: "uploading" })), ...previousFiles]);
const onComplete = (file, status, skylink) => { const onFileStateChange = (file, state) => {
setFiles((previousFiles) => { setFiles((previousFiles) => {
const index = previousFiles.findIndex((f) => f.file === file); const index = previousFiles.findIndex((f) => f.file === file);
@ -23,26 +24,33 @@ export default function HomeUpload() {
...previousFiles.slice(0, index), ...previousFiles.slice(0, index),
{ {
...previousFiles[index], ...previousFiles[index],
status, ...state
url: `${apiUrl}/${skylink}`
}, },
...previousFiles.slice(index + 1) ...previousFiles.slice(index + 1)
]; ];
}); });
}; };
const onProgress = (file, { loaded, total }) => {
const progress = loaded / total;
const status = progress === 1 ? "processing" : "uploading";
onFileStateChange(file, { status, progress });
};
acceptedFiles.forEach(async (file) => { acceptedFiles.forEach(async (file) => {
try { try {
const fd = new FormData(); const fd = new FormData();
fd.append("file", file); fd.append("file", file);
const uuid = shortid.generate(); const uuid = shortid.generate();
const response = await fetch(`${apiUrl}/skynet/skyfile/${uuid}`, { method: "POST", body: fd }); const { data } = await axios.post(`${apiUrl}/skynet/skyfile/${uuid}`, fd, {
const { skylink } = await response.json(); onUploadProgress: (event) => onProgress(file, event)
});
onComplete(file, "complete", skylink); onFileStateChange(file, { status: "complete", url: `${apiUrl}/${data.skylink}` });
} catch (error) { } catch (error) {
onComplete(file, "error"); onFileStateChange(file, { status: "error" });
} }
}); });
}; };

View File

@ -4,7 +4,7 @@ import "./UploadFile.scss";
import { LoadingSpinner } from "../"; import { LoadingSpinner } from "../";
import { File, FileCheck, FileError, Copy } from "../../svg"; import { File, FileCheck, FileError, Copy } from "../../svg";
export default function UploadFile({ file, url, status }) { export default function UploadFile({ file, url, status, progress }) {
const [copied, setCopied] = useState(false); const [copied, setCopied] = useState(false);
const urlRef = useRef(null); const urlRef = useRef(null);
@ -43,7 +43,7 @@ export default function UploadFile({ file, url, status }) {
<div className="upload-file-text"> <div className="upload-file-text">
<h3>{file.name}</h3> <h3>{file.name}</h3>
<p> <p>
{status === "uploading" && "Uploading..."} {status === "uploading" && (progress ? `Uploading ${Math.round(progress * 100)}%` : "Uploading...")}
{status === "processing" && "Processing..."} {status === "processing" && "Processing..."}
{status === "error" && <span className="red-text">Error processing file.</span>} {status === "error" && <span className="red-text">Error processing file.</span>}
{status === "complete" && ( {status === "complete" && (
@ -78,5 +78,6 @@ UploadFile.propTypes = {
name: PropTypes.string.isRequired name: PropTypes.string.isRequired
}), }),
status: PropTypes.string.isRequired, status: PropTypes.string.isRequired,
url: PropTypes.string url: PropTypes.string,
progress: PropTypes.number
}; };