moved endpoints to shovel

This commit is contained in:
Aminejvm 2021-03-05 08:32:08 +01:00
parent 952bb22008
commit 8fdc1962a1
10 changed files with 35 additions and 86 deletions

View File

@ -31,14 +31,15 @@ const returnJSON = async (route, options) => {
return json;
};
export const createZipToken = async (files) =>
await returnJSON("/api/download/create-zip-token", {
export const createZipToken = async ({ files, resourceURI }) => {
return await returnJSON(`${resourceURI}/api/download/create-zip-token`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ files }),
});
};
export const downloadZip = (downloadToken) =>
`/api/download/download-by-token?downloadId=${downloadToken}`;
export const downloadZip = ({ token, name, resourceURI }) =>
`${resourceURI}/api/download/download-by-token?downloadId=${token}&name=${name}`;
export const health = async (data = {}) => {
await Websockets.checkWebsocket();

View File

@ -333,17 +333,22 @@ export const downloadZip = async (file) => {
};
const _nativeDownload = (file) => {
var element = document.createElement("a");
element.setAttribute("href", file.url);
element.setAttribute("download", file.name);
const iframe = document.createElement("iframe");
iframe.style.display = "none";
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
iframe.src = file.url;
document.body.appendChild(iframe);
// var element = document.createElement("a");
// element.setAttribute("href", file.url);
// element.setAttribute("download", file.name);
// element.style.display = "none";
// document.body.appendChild(element);
// element.click();
// document.body.removeChild(element);
};
export const compressAndDownloadFiles = async (files, name = "slate.zip") => {
export const compressAndDownloadFiles = async ({ files, name = "slate.zip", resourceURI }) => {
try {
if (!(files && files.length > 0)) return;
Events.dispatchMessage({ message: "We're preparing your files to download", status: "INFO" });
@ -366,14 +371,14 @@ export const compressAndDownloadFiles = async (files, name = "slate.zip") => {
});
}
const res = await Actions.createZipToken(downloadFiles);
const downloadLink = Actions.downloadZip(res.data.token);
const res = await Actions.createZipToken({ files: downloadFiles, resourceURI });
const downloadLink = Actions.downloadZip({ token: res.data.token, name, resourceURI });
_nativeDownload({
name,
url: downloadLink,
});
} catch (e) {
console.error(e);
Events.dispatchMessage({ message: "Something went wrong with the download. Please try again" });
}
};

View File

@ -370,7 +370,10 @@ export default class DataView extends React.Component {
_handleDownloadFiles = async () => {
const selectedFiles = this.props.items.filter((_, i) => this.state.checked[i]);
UserBehaviors.compressAndDownloadFiles(selectedFiles);
UserBehaviors.compressAndDownloadFiles({
files: selectedFiles,
resourceURI: this.props.resources.download,
});
this.setState({ checked: {} });
};

View File

@ -685,6 +685,7 @@ export default class Profile extends React.Component {
items={this.state.publicFiles}
onUpdateViewer={this.props.onUpdateViewer}
view={this.state.view}
resources={this.props.resources}
/>
) : (
<EmptyState>

View File

@ -36,7 +36,6 @@
"@glif/filecoin-number": "^1.1.0-beta.17",
"@slack/webhook": "^6.0.0",
"@textile/hub": "^6.0.2",
"archiver": "^5.2.0",
"babel-plugin-module-resolver": "^4.1.0",
"bcrypt": "^5.0.0",
"blurhash": "^1.1.3",
@ -57,7 +56,6 @@
"moment": "^2.29.1",
"morgan": "^1.10.0",
"next": "^10.0.7",
"node-persist": "^3.1.0",
"pg": "^8.5.1",
"prismjs": "^1.23.0",
"react": "^17.0.1",
@ -69,7 +67,6 @@
"remark-linkify-regex": "^1.0.0",
"remark-parse": "^9.0.0",
"remark-react": "^8.0.0",
"request": "^2.88.2",
"three": "^0.108.0",
"unified": "^9.2.0",
"universal-cookie": "^4.0.4",

View File

@ -1,22 +0,0 @@
import { v4 } from "uuid";
import storage from "node-persist";
export default async (req, res) => {
if (!(req.body.files && req.body.files.length > 0)) {
return res.status(500).send({ decorator: "SERVER_CREATE_ZIP_TOKEN_INVALID_INPUT" });
}
const token = v4();
const files = req.body.files;
await storage.init();
await storage.setItem(token, JSON.stringify(files));
setTimeout(() => {
storage.removeItem(token);
}, 10 * 60 * 1000);
res.status(200).json({
decorator: "SERVER_CREATE_ZIP_TOKEN_SUCCESS",
data: { token },
});
};

View File

@ -1,44 +0,0 @@
import archiver from "archiver";
import Request from "request";
import storage from "node-persist";
const request = (link) => Request.get(link);
export default async (req, res) => {
const downloadId = req.query.downloadId;
let files = [];
try {
files = JSON.parse(await storage.getItem(downloadId));
} catch (e) {
if (!files)
return res.status(500).send({
decorator: "SERVER_DOWNLOAD_BY_TOKEN_INVALID",
});
}
const archive = archiver("zip");
try {
archive.on("warning", function (err) {
if (err.code === "ENOENT") {
// log warning
} else {
// throw error
throw err;
}
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(res);
files.forEach((file) => archive.append(request(file.url), { name: file.name }));
archive.finalize();
storage.removeItem(downloadId);
} catch (e) {
return res.status(500).send({
decorator: "SERVER_DOWNLOAD_BY_TOKEN_ERROR",
});
}
};

View File

@ -498,6 +498,7 @@ export default class SceneFilesFolder extends React.Component {
items={files}
onUpdateViewer={this.props.onUpdateViewer}
view={this.state.view}
resources={this.props.resources}
/>
) : (
<EmptyState>

View File

@ -433,7 +433,11 @@ class SlatePage extends React.Component {
_handleDownload = () => {
const slateName = this.props.current.data.name;
const slateFiles = this.props.current.data.objects;
UserBehaviors.compressAndDownloadFiles(slateFiles, `${slateName}.zip`);
UserBehaviors.compressAndDownloadFiles({
files: slateFiles,
name: `${slateName}.zip`,
resourceURI: this.props.resources.download,
});
};
render() {

View File

@ -59,6 +59,9 @@ const EXTERNAL_RESOURCES = {
uploadZip: Strings.isEmpty(Environment.RESOURCE_URI_UPLOAD)
? null
: Environment.RESOURCE_URI_STORAGE_UPLOAD,
download: Strings.isEmpty(Environment.RESOURCE_URI_UPLOAD)
? null
: Environment.RESOURCE_URI_STORAGE_UPLOAD,
pubsub: Strings.isEmpty(Environment.RESOURCE_URI_PUBSUB) ? null : Environment.RESOURCE_URI_PUBSUB,
search: Strings.isEmpty(Environment.RESOURCE_URI_SEARCH) ? null : Environment.RESOURCE_URI_SEARCH,
};