mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-23 22:12:19 +03:00
Merge pull request #485 from filecoin-project/@akuokojnr/fix-download
fix: unity game download bug
This commit is contained in:
commit
6023e7a94b
@ -299,3 +299,10 @@ export const getActivity = async () => {
|
||||
...DEFAULT_OPTIONS,
|
||||
});
|
||||
};
|
||||
|
||||
export const getZipFilePaths = async (data) => {
|
||||
return await returnJSON(`api/zip/get-paths`, {
|
||||
...DEFAULT_OPTIONS,
|
||||
body: JSON.stringify({ data }),
|
||||
});
|
||||
};
|
||||
|
@ -9,6 +9,8 @@ import * as FileUtilities from "~/common/file-utilities";
|
||||
import * as Events from "~/common/custom-events";
|
||||
|
||||
import Cookies from "universal-cookie";
|
||||
import JSZip from "jszip";
|
||||
import { saveAs } from "file-saver";
|
||||
|
||||
const cookies = new Cookies();
|
||||
|
||||
@ -253,6 +255,26 @@ export const download = (file) => {
|
||||
Window.saveAs(uri, filename);
|
||||
};
|
||||
|
||||
export const downloadZip = async (file) => {
|
||||
const { data } = await Actions.getZipFilePaths(file);
|
||||
const filesPaths = data.filesPaths.map((item) => item.replace(`/${file.id}/`, ""));
|
||||
const baseUrl = file.url;
|
||||
const zipFileName = file.file;
|
||||
|
||||
let zip = new JSZip();
|
||||
|
||||
for (let filePath of filesPaths) {
|
||||
let url = `${baseUrl}/${filePath}`;
|
||||
const blob = await Window.getBlobFromUrl(url);
|
||||
|
||||
zip.file(filePath, blob);
|
||||
}
|
||||
|
||||
zip.generateAsync({ type: "blob" }).then((blob) => {
|
||||
saveAs(blob, zipFileName);
|
||||
});
|
||||
};
|
||||
|
||||
// export const createSlate = async (data) => {
|
||||
// let response = await Actions.createSlate({
|
||||
// name: data.name,
|
||||
|
@ -117,3 +117,18 @@ export const debounce = (func, wait) => {
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
};
|
||||
|
||||
export const getBlobFromUrl = async (url) => {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: new Headers({
|
||||
Origin: location.origin,
|
||||
}),
|
||||
mode: "cors",
|
||||
});
|
||||
|
||||
return response.blob();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
@ -319,7 +319,11 @@ export default class CarouselSidebarData extends React.Component {
|
||||
};
|
||||
|
||||
_handleDownload = () => {
|
||||
UserBehaviors.download(this.props.data);
|
||||
if (this.props.data.type === "application/unity") {
|
||||
UserBehaviors.downloadZip(this.props.data);
|
||||
} else {
|
||||
UserBehaviors.download(this.props.data);
|
||||
}
|
||||
};
|
||||
|
||||
_handleCreateSlate = async () => {
|
||||
|
@ -37,6 +37,7 @@
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"express-rate-limit": "^5.1.3",
|
||||
"file-saver": "^2.0.5",
|
||||
"fs-extra": "^9.0.1",
|
||||
"heic2any": "0.0.3",
|
||||
"isomorphic-fetch": "^3.0.0",
|
||||
|
43
pages/api/zip/get-paths.js
Normal file
43
pages/api/zip/get-paths.js
Normal file
@ -0,0 +1,43 @@
|
||||
import * as Utilities from "~/node_common/utilities";
|
||||
import * as Data from "~/node_common/data";
|
||||
|
||||
export default async (req, res) => {
|
||||
const id = Utilities.getIdFromCookie(req);
|
||||
|
||||
const user = await Data.getUserById({
|
||||
id,
|
||||
});
|
||||
|
||||
if (!user || user.error) {
|
||||
return res
|
||||
.status(403)
|
||||
.send({ decorator: "SERVER_GET_ZIP_FILES_PATHS_USER_NOT_FOUND", error: true });
|
||||
}
|
||||
|
||||
let { buckets, bucketKey } = await Utilities.getBucketAPIFromUserToken({
|
||||
user,
|
||||
});
|
||||
|
||||
if (!buckets) {
|
||||
return res.status(500).send({
|
||||
decorator: "SERVER_GET_BUCKET_DATA",
|
||||
error: true,
|
||||
});
|
||||
}
|
||||
|
||||
let filesPaths = null;
|
||||
try {
|
||||
filesPaths = await buckets.listPathFlat(bucketKey, `/${req.body.data.id}`, false);
|
||||
} catch (e) {
|
||||
res.set("Connection", "close");
|
||||
return {
|
||||
decorator: "GET_ZIP_FILES_PATHS_BUCKET_CHECK_FAILED",
|
||||
error: true,
|
||||
};
|
||||
}
|
||||
|
||||
return res.status(200).send({
|
||||
decorator: "SERVER_ZIP_FILES_PATHS",
|
||||
data: { filesPaths },
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user