2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-25 21:38:40 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const default_max_file_size = 5;
|
2017-03-06 06:22:28 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const supported_types = ["image/jpeg", "image/png", "image/gif", "image/tiff"];
|
2017-10-27 15:42:06 +02:00
|
|
|
|
2021-07-06 19:27:48 +02:00
|
|
|
function is_image_format(file: File): boolean {
|
2019-11-02 00:06:25 +01:00
|
|
|
const type = file.type;
|
2019-10-25 09:15:16 +02:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
2017-02-21 03:15:22 +01:00
|
|
|
}
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
return supported_types.includes(type);
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2021-02-28 01:15:23 +01:00
|
|
|
export function build_widget(
|
|
|
|
// function returns a jQuery file input object
|
2021-07-06 19:27:48 +02:00
|
|
|
get_file_input: () => JQuery<HTMLInputElement>,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery object to show file name
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_name_field: JQuery,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery object for error text
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error: JQuery,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery button to clear last upload choice
|
2022-01-25 11:36:19 +01:00
|
|
|
$clear_button: JQuery,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery button to open file dialog
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button: JQuery,
|
|
|
|
$preview_text?: JQuery,
|
|
|
|
$preview_image?: JQuery,
|
2021-07-06 19:27:48 +02:00
|
|
|
max_file_upload_size = default_max_file_size,
|
|
|
|
): {clear(): void; close(): void} {
|
|
|
|
function accept(file: File): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_name_field.text(file.name);
|
|
|
|
$input_error.hide();
|
|
|
|
$clear_button.show();
|
|
|
|
$upload_button.hide();
|
|
|
|
if ($preview_text !== undefined && $preview_image !== undefined) {
|
2020-03-21 12:58:14 +01:00
|
|
|
const image_blob = URL.createObjectURL(file);
|
2022-01-25 11:36:19 +01:00
|
|
|
$preview_image.attr("src", image_blob);
|
|
|
|
$preview_text.show();
|
2020-03-21 12:58:14 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2021-07-06 19:27:48 +02:00
|
|
|
function clear(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $control = get_file_input();
|
|
|
|
$control.val("");
|
|
|
|
$file_name_field.text("");
|
|
|
|
$clear_button.hide();
|
|
|
|
$upload_button.show();
|
|
|
|
if ($preview_text !== undefined) {
|
|
|
|
$preview_text.hide();
|
2020-03-21 12:58:14 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$clear_button.on("click", (e) => {
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.on("drop", (e) => {
|
2021-07-06 19:27:48 +02:00
|
|
|
const files = e.originalEvent?.dataTransfer?.files;
|
2019-10-25 09:15:16 +02:00
|
|
|
if (files === null || files === undefined || files.length === 0) {
|
2017-02-21 03:15:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-05 01:19:48 +01:00
|
|
|
get_file_input()[0].files = files;
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
get_file_input().attr("accept", supported_types.toString());
|
|
|
|
get_file_input().on("change", (e) => {
|
2021-07-06 19:27:48 +02:00
|
|
|
if (e.target.files?.length === 0) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.hide();
|
2021-07-06 19:27:48 +02:00
|
|
|
} else if (e.target.files?.length === 1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const file = e.target.files[0];
|
2019-10-25 09:15:16 +02:00
|
|
|
if (file.size > max_file_upload_size * 1024 * 1024) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text(
|
2021-04-13 06:51:54 +02:00
|
|
|
$t(
|
2021-04-13 08:39:44 +02:00
|
|
|
{defaultMessage: "File size must be at most {max_file_size} MiB."},
|
2021-04-13 06:51:54 +02:00
|
|
|
{max_file_size: max_file_upload_size},
|
|
|
|
),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
|
|
|
} else if (!is_image_format(file)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text($t({defaultMessage: "File type is not supported."}));
|
|
|
|
$input_error.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
2019-01-27 08:25:10 +01:00
|
|
|
} else {
|
2019-10-25 09:15:16 +02:00
|
|
|
accept(file);
|
2019-01-27 08:25:10 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text($t({defaultMessage: "Please just upload one file."}));
|
2017-02-21 03:15:22 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.on("click", (e) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
get_file_input().trigger("click");
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2021-07-06 19:27:48 +02:00
|
|
|
function close(): void {
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
2022-01-25 11:36:19 +01:00
|
|
|
$clear_button.off("click");
|
|
|
|
$upload_button.off("drop");
|
2020-07-15 01:29:15 +02:00
|
|
|
get_file_input().off("change");
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.off("click");
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
return {
|
|
|
|
// Call back to clear() in situations like adding bots, when
|
|
|
|
// we want to use the same widget over and over again.
|
2020-07-20 22:18:43 +02:00
|
|
|
clear,
|
2019-10-25 09:15:16 +02:00
|
|
|
// Call back to close() when you are truly done with the widget,
|
|
|
|
// so you can release handlers.
|
2020-07-20 22:18:43 +02:00
|
|
|
close,
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2021-02-28 01:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function build_direct_upload_widget(
|
|
|
|
// function returns a jQuery file input object
|
2021-07-06 19:27:48 +02:00
|
|
|
get_file_input: () => JQuery<HTMLInputElement>,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery object for error text
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error: JQuery,
|
2021-02-28 01:15:23 +01:00
|
|
|
// jQuery button to open file dialog
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button: JQuery,
|
2021-07-06 19:27:48 +02:00
|
|
|
upload_function: (
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_input: JQuery<HTMLInputElement>,
|
2021-07-06 19:27:48 +02:00
|
|
|
night: boolean | null,
|
|
|
|
icon: boolean,
|
|
|
|
) => void,
|
|
|
|
max_file_upload_size = default_max_file_size,
|
|
|
|
): void {
|
2020-03-28 01:25:56 +01:00
|
|
|
// default value of max uploaded file size
|
2021-07-06 19:27:48 +02:00
|
|
|
function accept(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.hide();
|
|
|
|
const $realm_logo_section = $upload_button.closest(".image_upload_widget");
|
|
|
|
if ($realm_logo_section.attr("id") === "realm-night-logo-upload-widget") {
|
2020-06-11 01:04:12 +02:00
|
|
|
upload_function(get_file_input(), true, false);
|
2022-01-25 11:36:19 +01:00
|
|
|
} else if ($realm_logo_section.attr("id") === "realm-day-logo-upload-widget") {
|
2020-06-11 01:04:12 +02:00
|
|
|
upload_function(get_file_input(), false, false);
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2020-06-11 01:04:12 +02:00
|
|
|
upload_function(get_file_input(), null, true);
|
2017-02-21 03:15:22 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2021-07-06 19:27:48 +02:00
|
|
|
function clear(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $control = get_file_input();
|
|
|
|
$control.val("");
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.on("drop", (e) => {
|
2021-07-06 19:27:48 +02:00
|
|
|
const files = e.originalEvent?.dataTransfer?.files;
|
2019-10-25 09:15:16 +02:00
|
|
|
if (files === null || files === undefined || files.length === 0) {
|
2017-02-21 03:15:22 +01:00
|
|
|
return false;
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2021-11-05 01:19:48 +01:00
|
|
|
get_file_input()[0].files = files;
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
get_file_input().attr("accept", supported_types.toString());
|
|
|
|
get_file_input().on("change", (e) => {
|
2021-07-06 19:27:48 +02:00
|
|
|
if (e.target.files?.length === 0) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.hide();
|
2021-07-06 19:27:48 +02:00
|
|
|
} else if (e.target.files?.length === 1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const file = e.target.files[0];
|
2019-10-25 09:15:16 +02:00
|
|
|
if (file.size > max_file_upload_size * 1024 * 1024) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text(
|
2021-04-13 06:51:54 +02:00
|
|
|
$t(
|
2021-04-13 08:39:44 +02:00
|
|
|
{defaultMessage: "File size must be at most {max_file_size} MiB."},
|
2021-04-13 06:51:54 +02:00
|
|
|
{max_file_size: max_file_upload_size},
|
|
|
|
),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
|
|
|
} else if (!is_image_format(file)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text($t({defaultMessage: "File type is not supported."}));
|
|
|
|
$input_error.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
clear();
|
2017-02-21 03:15:22 +01:00
|
|
|
} else {
|
2019-10-25 09:15:16 +02:00
|
|
|
accept();
|
2017-02-21 03:15:22 +01:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_error.text($t({defaultMessage: "Please just upload one file."}));
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
|
|
|
});
|
2017-02-21 03:15:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.on("click", (e) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
get_file_input().trigger("click");
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2021-02-28 01:15:23 +01:00
|
|
|
}
|