2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2023-08-30 14:08:17 +02:00
|
|
|
import {z} from "zod";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2022-09-03 18:45:02 +02:00
|
|
|
import render_confirm_delete_attachment from "../templates/confirm_dialog/confirm_delete_attachment.hbs";
|
2021-02-28 01:09:13 +01:00
|
|
|
import render_settings_upload_space_stats from "../templates/settings/upload_space_stats.hbs";
|
2021-05-14 10:32:36 +02:00
|
|
|
import render_uploaded_files_list from "../templates/settings/uploaded_files_list.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:09:13 +01:00
|
|
|
import * as channel from "./channel";
|
2022-09-03 18:45:02 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2023-03-04 12:50:45 +01:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:09:13 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
2023-04-25 18:01:02 +02:00
|
|
|
import * as scroll_util from "./scroll_util";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {realm} from "./state_data";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2021-02-28 01:09:13 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-02-28 00:36:14 +01:00
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
type ServerAttachment = z.infer<typeof attachment_api_response_schema>["attachments"][number];
|
|
|
|
|
|
|
|
type Attachment = ServerAttachment & {
|
|
|
|
create_time_str: string;
|
|
|
|
size_str: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AttachmentEvent =
|
|
|
|
| {
|
|
|
|
op: "add" | "update";
|
|
|
|
attachment: ServerAttachment;
|
|
|
|
upload_space_used: number;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
op: "remove";
|
|
|
|
attachment: {id: number};
|
|
|
|
upload_space_used: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
const attachment_api_response_schema = z.object({
|
|
|
|
attachments: z.array(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
name: z.string(),
|
|
|
|
path_id: z.string(),
|
|
|
|
size: z.number(),
|
|
|
|
create_time: z.number(),
|
|
|
|
messages: z.array(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
date_sent: z.number(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
upload_space_used: z.number(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let attachments: Attachment[];
|
|
|
|
let upload_space_used: z.infer<typeof attachment_api_response_schema>["upload_space_used"];
|
|
|
|
|
|
|
|
export function bytes_to_size(bytes: number, kb_with_1024_bytes = false): string {
|
2019-11-02 00:06:25 +01:00
|
|
|
const kb_size = kb_with_1024_bytes ? 1024 : 1000;
|
2020-07-15 01:29:15 +02:00
|
|
|
const sizes = ["B", "KB", "MB", "GB", "TB"];
|
2017-09-17 01:20:11 +02:00
|
|
|
if (bytes === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "0 B";
|
2017-09-17 01:20:11 +02:00
|
|
|
}
|
2023-09-05 05:53:47 +02:00
|
|
|
const i = Math.trunc(Math.log(bytes) / Math.log(kb_size));
|
2019-11-02 00:06:25 +01:00
|
|
|
let size = Math.round(bytes / Math.pow(kb_size, i));
|
2018-06-06 18:50:09 +02:00
|
|
|
if (i > 0 && size < 10) {
|
2020-07-15 00:34:28 +02:00
|
|
|
size = Math.round((bytes / Math.pow(kb_size, i)) * 10) / 10;
|
2017-12-05 00:44:05 +01:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return size + " " + sizes[i];
|
2021-02-28 01:09:13 +01:00
|
|
|
}
|
2019-01-15 08:39:46 +01:00
|
|
|
|
2024-03-20 02:30:08 +01:00
|
|
|
export function mib_to_bytes(mib: number): number {
|
|
|
|
return mib * 1024 * 1024;
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
export function percentage_used_space(uploads_size: number): string | null {
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.realm_upload_quota_mib === null) {
|
2019-01-17 12:05:09 +01:00
|
|
|
return null;
|
|
|
|
}
|
2024-03-20 02:30:08 +01:00
|
|
|
return ((100 * uploads_size) / mib_to_bytes(realm.realm_upload_quota_mib)).toFixed(1);
|
2021-02-28 01:09:13 +01:00
|
|
|
}
|
2019-01-17 12:05:09 +01:00
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
function set_upload_space_stats(): void {
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.realm_upload_quota_mib === null) {
|
2019-01-17 12:05:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = {
|
2024-02-13 02:08:24 +01:00
|
|
|
show_upgrade_message: realm.realm_plan_type === 2,
|
2021-02-28 01:09:13 +01:00
|
|
|
percent_used: percentage_used_space(upload_space_used),
|
2024-03-20 02:30:08 +01:00
|
|
|
upload_quota: bytes_to_size(mib_to_bytes(realm.realm_upload_quota_mib), true),
|
2019-01-17 12:05:09 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_upload_stats_html = render_settings_upload_space_stats(args);
|
2019-01-17 12:05:09 +01:00
|
|
|
$("#attachment-stats-holder").html(rendered_upload_stats_html);
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
function delete_attachments(attachment: string, file_name: string): void {
|
2022-09-03 18:45:02 +02:00
|
|
|
const html_body = render_confirm_delete_attachment({file_name});
|
|
|
|
|
|
|
|
dialog_widget.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Delete file?"}),
|
|
|
|
html_body,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Delete"}),
|
|
|
|
id: "confirm_delete_file_modal",
|
|
|
|
focus_submit_on_open: true,
|
2023-12-22 01:00:27 +01:00
|
|
|
on_click() {
|
2024-04-04 03:36:46 +02:00
|
|
|
dialog_widget.submit_api_request(channel.del, "/json/attachments/" + attachment, {});
|
2023-12-22 01:00:27 +01:00
|
|
|
},
|
2022-09-03 18:45:02 +02:00
|
|
|
loading_spinner: true,
|
2019-01-15 08:39:46 +01:00
|
|
|
});
|
2018-05-05 00:48:29 +02:00
|
|
|
}
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
function sort_mentioned_in(a: Attachment, b: Attachment): number {
|
2020-04-12 12:13:47 +02:00
|
|
|
const a_m = a.messages[0];
|
|
|
|
const b_m = b.messages[0];
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (!a_m) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!b_m) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-12 12:13:47 +02:00
|
|
|
|
|
|
|
if (a_m.id > b_m.id) {
|
|
|
|
return 1;
|
|
|
|
} else if (a_m.id === b_m.id) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
function render_attachments_ui(): void {
|
2019-01-17 12:05:09 +01:00
|
|
|
set_upload_space_stats();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $uploaded_files_table = $("#uploaded_files_table").expectOne();
|
2023-09-08 22:52:44 +02:00
|
|
|
const $search_input = $<HTMLInputElement>("input#upload_file_search");
|
2017-02-24 02:31:37 +01:00
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
ListWidget.create<Attachment>($uploaded_files_table, attachments, {
|
2017-09-28 21:07:23 +02:00
|
|
|
name: "uploaded-files-list",
|
2023-05-01 13:44:40 +02:00
|
|
|
get_item: ListWidget.default_get_item,
|
2023-09-16 00:03:52 +02:00
|
|
|
modifier_html(attachment) {
|
2020-07-20 22:18:43 +02:00
|
|
|
return render_uploaded_files_list({attachment});
|
2017-09-28 21:07:23 +02:00
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $search_input,
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
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 item.name.toLocaleLowerCase().includes(value);
|
2017-09-28 21:07:23 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
onupdate() {
|
2023-04-25 18:01:02 +02:00
|
|
|
scroll_util.reset_scrollbar(
|
|
|
|
$uploaded_files_table.closest(".progressive-table-wrapper"),
|
|
|
|
);
|
2017-09-28 21:08:43 +02:00
|
|
|
},
|
2017-09-28 21:07:23 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $("#attachments-settings").expectOne(),
|
2023-05-03 07:06:19 +02:00
|
|
|
init_sort: "create_time_numeric",
|
2023-02-24 00:56:50 +01:00
|
|
|
initially_descending_sort: true,
|
2020-04-11 16:23:29 +02:00
|
|
|
sort_fields: {
|
|
|
|
mentioned_in: sort_mentioned_in,
|
2023-05-03 07:06:19 +02:00
|
|
|
...ListWidget.generic_sort_functions("alphabetic", ["name"]),
|
|
|
|
...ListWidget.generic_sort_functions("numeric", ["create_time", "size"]),
|
2020-04-11 16:23:29 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $("#attachments-settings .progressive-table-wrapper"),
|
2020-04-11 16:23:29 +02:00
|
|
|
});
|
2018-09-29 18:19:03 +02:00
|
|
|
|
2023-04-25 18:01:02 +02:00
|
|
|
scroll_util.reset_scrollbar($uploaded_files_table.closest(".progressive-table-wrapper"));
|
2018-05-05 00:27:25 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 19:07:58 +02:00
|
|
|
function format_attachment_data(attachment: ServerAttachment): Attachment {
|
|
|
|
return {
|
2023-08-30 13:47:35 +02:00
|
|
|
...attachment,
|
|
|
|
create_time_str: timerender.render_now(new Date(attachment.create_time)).time_str,
|
|
|
|
size_str: bytes_to_size(attachment.size),
|
2024-05-30 19:07:58 +02:00
|
|
|
};
|
2018-05-05 01:10:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
export function update_attachments(event: AttachmentEvent): void {
|
2018-05-05 00:48:29 +02:00
|
|
|
if (attachments === undefined) {
|
|
|
|
// If we haven't fetched attachment data yet, there's nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event.op === "remove" || event.op === "update") {
|
2020-07-02 01:45:54 +02:00
|
|
|
attachments = attachments.filter((a) => a.id !== event.attachment.id);
|
2018-05-05 00:48:29 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event.op === "add" || event.op === "update") {
|
2024-05-30 19:07:58 +02:00
|
|
|
attachments.push(format_attachment_data(event.attachment));
|
2018-05-05 00:48:29 +02:00
|
|
|
}
|
2019-01-17 12:05:09 +01:00
|
|
|
upload_space_used = event.upload_space_used;
|
2018-05-05 00:48:29 +02:00
|
|
|
// TODO: This is inefficient and we should be able to do some sort
|
2021-01-29 10:27:56 +01:00
|
|
|
// of incremental ListWidget update instead.
|
2018-05-05 00:48:29 +02:00
|
|
|
render_attachments_ui();
|
2021-02-28 01:09:13 +01:00
|
|
|
}
|
2018-05-05 00:48:29 +02:00
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
export function set_up_attachments(): void {
|
2018-05-05 00:27:25 +02:00
|
|
|
// The settings page must be rendered before this function gets called.
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $status = $("#delete-upload-status");
|
2023-03-04 12:50:45 +01:00
|
|
|
loading.make_indicator($("#attachments_loading_indicator"), {
|
|
|
|
text: $t({defaultMessage: "Loading…"}),
|
|
|
|
});
|
2018-05-05 00:27:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#uploaded_files_table").on("click", ".remove-attachment", (e) => {
|
2022-09-03 18:45:02 +02:00
|
|
|
const file_name = $(e.target).closest(".uploaded_file_row").attr("id");
|
|
|
|
delete_attachments(
|
2023-08-30 14:08:17 +02:00
|
|
|
$(e.target).closest(".uploaded_file_row").attr("data-attachment-id")!,
|
|
|
|
file_name!,
|
2022-09-03 18:45:02 +02:00
|
|
|
);
|
2018-05-05 01:05:16 +02:00
|
|
|
});
|
|
|
|
|
2023-08-30 14:08:17 +02:00
|
|
|
void channel.get({
|
2018-05-05 00:27:25 +02:00
|
|
|
url: "/json/attachments",
|
2024-06-21 20:30:52 +02:00
|
|
|
success(raw_data) {
|
|
|
|
const data = attachment_api_response_schema.parse(raw_data);
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#attachments_loading_indicator"));
|
2024-06-21 20:30:52 +02:00
|
|
|
attachments = data.attachments.map((attachment) => format_attachment_data(attachment));
|
|
|
|
upload_space_used = data.upload_space_used;
|
2018-05-05 01:12:04 +02:00
|
|
|
render_attachments_ui();
|
2018-05-05 00:27:25 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#attachments_loading_indicator"));
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, $status);
|
2018-05-05 00:27:25 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 01:09:13 +01:00
|
|
|
}
|