IO-1590 Production by Technician IO-1585 File access api

This commit is contained in:
Patrick Fic
2021-12-24 09:30:26 -08:00
parent 74c6d32849
commit 7abc3976e8
8 changed files with 143 additions and 37 deletions

View File

@@ -172,8 +172,7 @@ export default function GlobalSearch() {
options={options}
onSearch={handleSearch}
placeholder={t("general.labels.globalsearch")}
>
<Input.Search loading={loading} />
</AutoComplete>
allowClear
></AutoComplete>
);
}

View File

@@ -5,45 +5,124 @@ import { useTranslation } from "react-i18next";
import { logImEXEvent } from "../../firebase/firebase.utils";
import cleanAxios from "../../utils/CleanAxios";
import formatBytes from "../../utils/formatbytes";
import yauzl from "yauzl";
import { useTreatments } from "@splitsoftware/splitio-react";
export default function JobsDocumentsDownloadButton({
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(JobsDocumentsDownloadButton);
export function JobsDocumentsDownloadButton({
bodyshop,
galleryImages,
identifier,
}) {
const { t } = useTranslation();
const [download, setDownload] = useState(null);
const { Direct_Media_Download } = useTreatments(
["Direct_Media_Download"],
{},
bodyshop.imexshopid
);
const imagesToDownload = [
...galleryImages.images.filter((image) => image.isSelected),
// ...galleryImages.other.filter((image) => image.isSelected),
];
const handleDownload = () => {
logImEXEvent("jobs_documents_download");
axios
.post("/media/download", {
ids: imagesToDownload.map((_) => _.key),
})
.then((r) => {
// window.open(r.data);
downloadAs(
r.data,
`${identifier || "documents"}.zip`,
(progressEvent) => {
setDownload((currentDownloadState) => {
return {
downloaded: progressEvent.loaded || 0,
speed:
(progressEvent.loaded || 0) -
((currentDownloadState && currentDownloadState.downloaded) ||
0),
};
});
},
() => setDownload(null)
);
});
};
function downloadProgress(progressEvent) {
setDownload((currentDownloadState) => {
return {
downloaded: progressEvent.loaded || 0,
speed:
(progressEvent.loaded || 0) -
((currentDownloadState && currentDownloadState.downloaded) || 0),
};
});
}
const handleDownload = async () => {
logImEXEvent("jobs_documents_download");
const zipUrl = await axios({
url: "/media/download",
method: "POST",
//responseType: "arraybuffer", // Important
data: { ids: imagesToDownload.map((_) => _.key) },
});
const theDownloadedZip = await cleanAxios({
url: zipUrl.data,
method: "GET",
responseType: "arraybuffer",
onDownloadProgress: downloadProgress,
});
setDownload(null);
if (Direct_Media_Download.treatment === "on") {
try {
const parentDir = await window.showDirectoryPicker({
id: "media",
startIn: "downloads",
});
const directory = await parentDir.getDirectoryHandle(identifier, {
create: true,
});
yauzl.fromBuffer(
Buffer.from(theDownloadedZip.data),
{},
(err, zipFile) => {
if (err) throw err;
zipFile.on("entry", (entry) => {
zipFile.openReadStream(entry, async (readErr, readStream) => {
if (readErr) {
zipFile.close();
throw readErr;
}
if (err) throw err;
let fileSystemHandle = await directory.getFileHandle(
entry.fileName,
{
create: true,
}
);
const writable = await fileSystemHandle.createWritable();
readStream.on("data", async function (chunk) {
await writable.write(chunk);
});
readStream.on("end", async function () {
await writable.close();
});
});
});
}
);
} catch (e) {
console.log(e);
standardMediaDownload(theDownloadedZip.data);
}
} else {
standardMediaDownload(theDownloadedZip.data);
}
function standardMediaDownload(bufferData) {
const a = document.createElement("a");
const url = window.URL.createObjectURL(new Blob([bufferData]));
a.href = url;
a.download = `${identifier || "documents"}.zip`;
a.click();
}
};
return (
<>
<Button
@@ -75,11 +154,6 @@ const downloadAs = (url, name, onDownloadProgress, onCompleted) => {
})
.then((response) => {
onCompleted && onCompleted();
const a = document.createElement("a");
const url = window.URL.createObjectURL(response.data);
a.href = url;
a.download = name;
a.click();
})
.catch((err) => {
console.log("error", err);