2020-09-27 00:15:37 +03:00
|
|
|
import * as Actions from "~/common/actions";
|
2020-10-23 13:34:31 +03:00
|
|
|
import * as Credentials from "~/common/credentials";
|
2020-10-31 02:12:20 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
2020-11-15 19:41:35 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-11-28 07:39:01 +03:00
|
|
|
import * as Events from "~/common/custom-events";
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2021-08-04 20:59:25 +03:00
|
|
|
import * as Environment from "~/common/environment";
|
2021-09-25 04:20:28 +03:00
|
|
|
import * as Constants from "~/common/constants";
|
2020-10-02 07:24:10 +03:00
|
|
|
|
2021-09-03 23:02:00 +03:00
|
|
|
import { encode, isBlurhashValid } from "blurhash";
|
2021-08-04 20:59:25 +03:00
|
|
|
import { v4 as uuid } from "uuid";
|
2020-09-12 01:25:33 +03:00
|
|
|
|
2021-07-07 23:50:57 +03:00
|
|
|
export const fileKey = ({ lastModified, name }) => `${lastModified}-${name}`;
|
|
|
|
|
2020-10-05 00:30:28 +03:00
|
|
|
const loadImage = async (src) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.crossOrigin = "Anonymous";
|
|
|
|
img.onload = () => resolve(img);
|
|
|
|
img.onerror = (...args) => reject(args);
|
|
|
|
img.src = src;
|
|
|
|
});
|
|
|
|
|
|
|
|
const getImageData = (image) => {
|
2020-12-12 06:57:54 +03:00
|
|
|
let ratio = Math.min(100 / image.height, 100 / image.width);
|
|
|
|
image.height = image.height * ratio;
|
|
|
|
image.width = image.width * ratio;
|
2020-10-05 00:30:28 +03:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = image.width;
|
|
|
|
canvas.height = image.height;
|
|
|
|
const context = canvas.getContext("2d");
|
2020-12-12 06:57:54 +03:00
|
|
|
context.scale(ratio, ratio);
|
2020-10-05 00:30:28 +03:00
|
|
|
context.drawImage(image, 0, 0);
|
|
|
|
return context.getImageData(0, 0, image.width, image.height);
|
|
|
|
};
|
|
|
|
|
|
|
|
const encodeImageToBlurhash = async (imageUrl) => {
|
|
|
|
const image = await loadImage(imageUrl);
|
|
|
|
const imageData = getImageData(image);
|
|
|
|
return encode(imageData.data, imageData.width, imageData.height, 4, 4);
|
|
|
|
};
|
|
|
|
|
2020-10-23 13:34:31 +03:00
|
|
|
// NOTE(jim): We're speaking to a different server now.
|
|
|
|
const getCookie = (name) => {
|
|
|
|
var match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
|
|
|
|
if (match) return match[2];
|
|
|
|
};
|
|
|
|
|
2021-09-22 14:12:23 +03:00
|
|
|
export const uploadLink = async ({ url, slate, uploadAbort }) => {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
if (uploadAbort) uploadAbort.abort = abortController.abort.bind(abortController);
|
2021-07-07 23:50:57 +03:00
|
|
|
|
2021-09-22 14:12:23 +03:00
|
|
|
let createResponse = await Actions.createLink({ url, slate }, { signal: abortController.signal });
|
|
|
|
return createResponse;
|
2021-07-07 23:50:57 +03:00
|
|
|
};
|
|
|
|
|
2021-11-22 20:29:13 +03:00
|
|
|
export const saveCopy = async ({ file, uploadAbort }) => {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
if (uploadAbort) uploadAbort.abort = abortController.abort.bind(abortController);
|
|
|
|
|
|
|
|
return await Actions.saveCopy({ files: [file] }, { signal: abortController.signal });
|
|
|
|
};
|
|
|
|
|
2021-09-15 20:56:24 +03:00
|
|
|
export const upload = async ({ file, onProgress, bucketName, uploadAbort }) => {
|
2020-08-20 08:20:18 +03:00
|
|
|
let formData = new FormData();
|
|
|
|
const HEIC2ANY = require("heic2any");
|
|
|
|
|
2020-08-20 08:29:33 +03:00
|
|
|
// NOTE(jim): You must provide a file from an type="file" input field.
|
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-15 19:41:35 +03:00
|
|
|
const isZipFile =
|
2020-11-14 16:27:45 +03:00
|
|
|
file.type.startsWith("application/zip") || file.type.startsWith("application/x-zip-compressed");
|
2020-11-15 19:41:35 +03:00
|
|
|
const isUnityFile = await Validations.isUnityFile(file);
|
2020-11-14 16:27:45 +03:00
|
|
|
|
2020-08-20 08:20:18 +03:00
|
|
|
// TODO(jim): Put this somewhere else to handle conversion cases.
|
|
|
|
if (file.type.startsWith("image/heic")) {
|
|
|
|
const converted = await HEIC2ANY({
|
|
|
|
blob: file,
|
|
|
|
toType: "image/png",
|
|
|
|
quality: 1,
|
2020-10-02 07:24:10 +03:00
|
|
|
}); //TODO(martina): figure out how to cancel an await if upload has been cancelled
|
2020-08-20 08:20:18 +03:00
|
|
|
|
|
|
|
formData.append("data", converted);
|
|
|
|
} else {
|
|
|
|
formData.append("data", file);
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:24:10 +03:00
|
|
|
const _privateUploadMethod = (path, file) =>
|
2021-08-04 20:59:25 +03:00
|
|
|
new Promise((resolve) => {
|
2020-08-20 08:20:18 +03:00
|
|
|
const XHR = new XMLHttpRequest();
|
2020-10-09 13:43:56 +03:00
|
|
|
|
2021-09-15 20:56:24 +03:00
|
|
|
if (uploadAbort) uploadAbort.abort = XHR.abort.bind(XHR);
|
2020-10-09 13:43:56 +03:00
|
|
|
|
2020-08-20 08:20:18 +03:00
|
|
|
XHR.open("post", path, true);
|
2020-10-23 13:34:31 +03:00
|
|
|
XHR.setRequestHeader("authorization", getCookie(Credentials.session.key));
|
2020-08-20 08:20:18 +03:00
|
|
|
XHR.onerror = (event) => {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.error(event);
|
2020-10-09 05:32:30 +03:00
|
|
|
XHR.abort();
|
2020-08-20 08:20:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE(jim): UPLOADS ONLY.
|
|
|
|
XHR.upload.addEventListener(
|
|
|
|
"progress",
|
|
|
|
(event) => {
|
|
|
|
if (event.lengthComputable) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("FILE UPLOAD PROGRESS", event);
|
2021-09-15 20:56:24 +03:00
|
|
|
if (onProgress) onProgress(event);
|
2020-08-20 08:20:18 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2021-09-15 20:56:24 +03:00
|
|
|
XHR.addEventListener("abort", () => {
|
|
|
|
resolve({ aborted: true });
|
|
|
|
});
|
2020-10-02 07:24:10 +03:00
|
|
|
|
2020-08-20 08:20:18 +03:00
|
|
|
XHR.onloadend = (event) => {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("FILE UPLOAD END", event);
|
2020-08-20 08:20:18 +03:00
|
|
|
try {
|
|
|
|
return resolve(JSON.parse(event.target.response));
|
|
|
|
} catch (e) {
|
|
|
|
return resolve({
|
|
|
|
error: "SERVER_UPLOAD_ERROR",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2020-09-19 22:36:58 +03:00
|
|
|
XHR.send(formData);
|
2020-08-20 08:20:18 +03:00
|
|
|
});
|
2021-08-04 20:59:25 +03:00
|
|
|
|
|
|
|
const storageDealRoute = `${Environment.URI_SHOVEL}/api/deal/`;
|
|
|
|
const generalRoute = `${Environment.URI_SHOVEL}/api/data/`;
|
|
|
|
const zipUploadRoute = `${Environment.URI_SHOVEL}/api/data/zip/`;
|
2020-10-23 13:34:31 +03:00
|
|
|
|
2020-11-14 16:27:45 +03:00
|
|
|
if (!storageDealRoute || !generalRoute || !zipUploadRoute) {
|
2020-11-28 07:39:01 +03:00
|
|
|
Events.dispatchMessage({ message: "We could not find our upload server." });
|
2020-10-27 21:54:57 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
decorator: "NO_UPLOAD_RESOURCE_URI_ATTACHED",
|
|
|
|
error: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let res;
|
2020-11-15 19:41:35 +03:00
|
|
|
if (isZipFile && isUnityFile) {
|
2020-11-14 16:27:45 +03:00
|
|
|
res = await _privateUploadMethod(`${zipUploadRoute}${file.name}`, file);
|
2021-09-25 04:20:28 +03:00
|
|
|
} else if (bucketName && bucketName === Constants.textile.dealsBucket) {
|
2020-10-23 13:34:31 +03:00
|
|
|
res = await _privateUploadMethod(`${storageDealRoute}${file.name}`, file);
|
2020-09-23 14:17:56 +03:00
|
|
|
} else {
|
2020-10-23 13:34:31 +03:00
|
|
|
res = await _privateUploadMethod(`${generalRoute}${file.name}`, file);
|
2020-09-23 14:17:56 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 20:56:24 +03:00
|
|
|
if (!res || res.error || res.aborted) {
|
|
|
|
return res;
|
2020-08-20 08:20:18 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let item = res.data.data;
|
2021-09-18 03:09:10 +03:00
|
|
|
if (Validations.isPreviewableImage(item.type)) {
|
2021-03-07 23:53:54 +03:00
|
|
|
let url = Strings.getURLfromCID(item.cid);
|
2020-11-02 23:27:11 +03:00
|
|
|
try {
|
|
|
|
let blurhash = await encodeImageToBlurhash(url);
|
2021-09-03 23:02:00 +03:00
|
|
|
if (isBlurhashValid(blurhash).result) {
|
2021-08-31 01:07:08 +03:00
|
|
|
item.blurhash = blurhash;
|
2021-09-03 23:02:00 +03:00
|
|
|
}
|
2020-11-02 23:27:11 +03:00
|
|
|
} catch (e) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.error(e);
|
2020-11-02 23:27:11 +03:00
|
|
|
}
|
2020-10-05 00:30:28 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
return item;
|
2020-08-20 08:20:18 +03:00
|
|
|
};
|
2021-07-07 23:50:57 +03:00
|
|
|
|
|
|
|
export const formatPastedImages = ({ clipboardItems }) => {
|
|
|
|
let files = [];
|
|
|
|
for (let i = 0; i < clipboardItems.length; i++) {
|
|
|
|
// Note(Amine): skip content if it's not an image
|
|
|
|
if (clipboardItems[i].type.indexOf("image") === -1) continue;
|
|
|
|
const file = clipboardItems[i].getAsFile();
|
|
|
|
files.push(file);
|
|
|
|
}
|
2021-09-15 20:56:24 +03:00
|
|
|
return { files };
|
2021-07-07 23:50:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const formatDroppedFiles = async ({ dataTransfer }) => {
|
|
|
|
// NOTE(jim): If this is true, then drag and drop came from a slate object.
|
|
|
|
const data = dataTransfer.getData("slate-object-drag-data");
|
|
|
|
if (data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = [];
|
|
|
|
if (dataTransfer.items && dataTransfer.items.length) {
|
|
|
|
for (var i = 0; i < dataTransfer.items.length; i++) {
|
|
|
|
const data = dataTransfer.items[i];
|
|
|
|
|
|
|
|
let file = null;
|
|
|
|
if (data.kind === "file") {
|
|
|
|
file = data.getAsFile();
|
|
|
|
} else if (data.kind == "string" && data.type == "text/uri-list") {
|
|
|
|
try {
|
2021-08-04 20:59:25 +03:00
|
|
|
const dataAsString = new Promise((resolve) => data.getAsString((d) => resolve(d)));
|
2021-07-07 23:50:57 +03:00
|
|
|
const resp = await fetch(await dataAsString);
|
|
|
|
const blob = resp.blob();
|
|
|
|
|
|
|
|
file = new File(blob, `data-${uuid()}`);
|
|
|
|
file.name = `data-${uuid()}`;
|
|
|
|
} catch (e) {
|
|
|
|
Events.dispatchMessage({
|
|
|
|
message: "File type not supported. Please try a different file",
|
|
|
|
});
|
|
|
|
|
|
|
|
return { error: true };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
files.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 20:56:24 +03:00
|
|
|
return { files };
|
2021-07-07 23:50:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const formatUploadedFiles = ({ files }) => {
|
|
|
|
let toUpload = [];
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
let file = files[i];
|
|
|
|
if (!file) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
toUpload.push(file);
|
|
|
|
}
|
|
|
|
|
2021-09-15 20:56:24 +03:00
|
|
|
return { files: toUpload };
|
2021-07-07 23:50:57 +03:00
|
|
|
};
|