2019-11-02 00:06:25 +01:00
|
|
|
const render_settings_upload_space_stats = require("../templates/settings/upload_space_stats.hbs");
|
|
|
|
const render_uploaded_files_list = require("../templates/uploaded_files_list.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let attachments;
|
|
|
|
let upload_space_used;
|
2018-05-05 01:12:04 +02:00
|
|
|
|
2019-01-15 08:39:46 +01:00
|
|
|
exports.bytes_to_size = function (bytes, kb_with_1024_bytes) {
|
2017-12-08 20:49:00 +01:00
|
|
|
if (kb_with_1024_bytes === undefined) {
|
|
|
|
kb_with_1024_bytes = false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const kb_size = kb_with_1024_bytes ? 1024 : 1000;
|
|
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
2017-09-17 01:20:11 +02:00
|
|
|
if (bytes === 0) {
|
|
|
|
return '0 B';
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(kb_size)), 10);
|
|
|
|
let size = Math.round(bytes / Math.pow(kb_size, i));
|
2018-06-06 18:50:09 +02:00
|
|
|
if (i > 0 && size < 10) {
|
|
|
|
size = Math.round(bytes / Math.pow(kb_size, i) * 10) / 10;
|
2017-12-05 00:44:05 +01:00
|
|
|
}
|
|
|
|
return size + ' ' + sizes[i];
|
2019-01-15 08:39:46 +01:00
|
|
|
};
|
|
|
|
|
2019-01-17 12:05:09 +01:00
|
|
|
exports.percentage_used_space = function (uploads_size) {
|
|
|
|
if (page_params.realm_upload_quota === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (100 * uploads_size / page_params.realm_upload_quota).toFixed(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function set_upload_space_stats() {
|
|
|
|
if (page_params.realm_upload_quota === null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = {
|
2019-01-17 12:05:09 +01:00
|
|
|
show_upgrade_message: page_params.realm_plan_type === 2,
|
|
|
|
percent_used: exports.percentage_used_space(upload_space_used),
|
2019-10-25 09:45:13 +02:00
|
|
|
upload_quota: exports.bytes_to_size(page_params.realm_upload_quota, 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);
|
|
|
|
}
|
|
|
|
|
2019-01-15 08:39:46 +01:00
|
|
|
function delete_attachments(attachment) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const status = $('#delete-upload-status');
|
2019-01-15 08:39:46 +01:00
|
|
|
channel.del({
|
|
|
|
url: '/json/attachments/' + attachment,
|
|
|
|
idempotent: true,
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, status);
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
ui_report.success(i18n.t("Attachment deleted"), status);
|
|
|
|
},
|
|
|
|
});
|
2018-05-05 00:48:29 +02:00
|
|
|
}
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2020-04-12 12:13:47 +02:00
|
|
|
function sort_mentioned_in(a, b) {
|
|
|
|
const a_m = a.messages[0];
|
|
|
|
const b_m = b.messages[0];
|
|
|
|
|
|
|
|
if (!a_m) { return 1; }
|
|
|
|
if (!b_m) { return -1; }
|
|
|
|
|
|
|
|
if (a_m.id > b_m.id) {
|
|
|
|
return 1;
|
|
|
|
} else if (a_m.id === b_m.id) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:12:04 +02:00
|
|
|
function render_attachments_ui() {
|
2019-01-17 12:05:09 +01:00
|
|
|
set_upload_space_stats();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const uploaded_files_table = $("#uploaded_files_table").expectOne();
|
|
|
|
const $search_input = $("#upload_file_search");
|
2017-02-24 02:31:37 +01:00
|
|
|
|
2020-04-11 16:23:29 +02:00
|
|
|
list_render.create(uploaded_files_table, attachments, {
|
2017-09-28 21:07:23 +02:00
|
|
|
name: "uploaded-files-list",
|
|
|
|
modifier: function (attachment) {
|
2019-07-09 21:24:00 +02:00
|
|
|
return render_uploaded_files_list({ attachment: attachment });
|
2017-09-28 21:07:23 +02:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $search_input,
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: function (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
|
|
|
},
|
2017-09-28 21:08:43 +02:00
|
|
|
onupdate: function () {
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.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
|
|
|
},
|
2018-06-21 09:16:05 +02:00
|
|
|
parent_container: $('#attachments-settings').expectOne(),
|
2020-04-11 16:23:29 +02:00
|
|
|
init_sort: ['numeric', 'create_time'],
|
|
|
|
sort_fields: {
|
|
|
|
mentioned_in: sort_mentioned_in,
|
|
|
|
},
|
|
|
|
});
|
2018-09-29 18:19:03 +02:00
|
|
|
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar(uploaded_files_table.closest(".progressive-table-wrapper"));
|
2018-05-05 00:27:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 01:10:46 +02:00
|
|
|
function format_attachment_data(new_attachments) {
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
for (const attachment of new_attachments) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const time = new XDate(attachment.create_time);
|
2018-05-05 01:10:46 +02:00
|
|
|
attachment.create_time_str = timerender.render_now(time).time_str;
|
2019-01-15 08:39:46 +01:00
|
|
|
attachment.size_str = exports.bytes_to_size(attachment.size);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
}
|
2018-05-05 01:10:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 00:48:29 +02:00
|
|
|
exports.update_attachments = function (event) {
|
|
|
|
if (attachments === undefined) {
|
|
|
|
// If we haven't fetched attachment data yet, there's nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
if (event.op === 'add' || event.op === 'update') {
|
|
|
|
format_attachment_data([event.attachment]);
|
|
|
|
attachments.push(event.attachment);
|
|
|
|
}
|
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
|
|
|
|
// of incremental list_render update instead.
|
|
|
|
render_attachments_ui();
|
|
|
|
};
|
|
|
|
|
2018-05-05 00:27:25 +02:00
|
|
|
exports.set_up_attachments = function () {
|
|
|
|
// The settings page must be rendered before this function gets called.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const status = $('#delete-upload-status');
|
2018-05-05 00:27:25 +02:00
|
|
|
loading.make_indicator($('#attachments_loading_indicator'), {text: 'Loading...'});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$('#uploaded_files_table').on('click', '.remove-attachment', (e) => {
|
2018-05-05 00:48:29 +02:00
|
|
|
delete_attachments($(e.target).closest(".uploaded_file_row").attr('data-attachment-id'));
|
2018-05-05 01:05:16 +02:00
|
|
|
});
|
|
|
|
|
2018-05-05 00:27:25 +02:00
|
|
|
channel.get({
|
|
|
|
url: "/json/attachments",
|
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
|
|
|
loading.destroy_indicator($('#attachments_loading_indicator'));
|
2018-05-05 01:10:46 +02:00
|
|
|
format_attachment_data(data.attachments);
|
2018-05-05 01:12:04 +02:00
|
|
|
attachments = data.attachments;
|
2019-01-17 12:05:09 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
loading.destroy_indicator($('#attachments_loading_indicator'));
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, status);
|
|
|
|
},
|
|
|
|
});
|
2016-12-28 14:46:42 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.attachments_ui = exports;
|