Ver a proveniência

feat: Optimize saved image

version-14
MitulDavid há 4 anos
ascendente
cometimento
baf7920fff
2 ficheiros alterados com 35 adições e 0 eliminações
  1. +19
    -0
      frappe/core/doctype/file/file.js
  2. +16
    -0
      frappe/core/doctype/file/file.py

+ 19
- 0
frappe/core/doctype/file/file.js Ver ficheiro

@@ -23,6 +23,25 @@ frappe.ui.form.on("File", "refresh", function(frm) {
wrapper.empty();
}

var is_raster_image = (/\.(gif|jpg|jpeg|tiff|png)$/i).test(frm.doc.file_url);
var is_optimizable = !frm.doc.is_folder && is_raster_image && frm.doc.file_size > 0;

if(is_optimizable) {
frm.add_custom_button(__("Optimize"), function() {
frappe.show_alert(__("Optimizing image..."));
frappe.call({
method: "frappe.core.doctype.file.file.optimize_saved_image",
args: {
doc_name: frm.doc.name,
},
callback: function() {
frappe.show_alert(__("Image optimized"));
frappe.set_route("List", "File");
}
});
});
}

if(frm.doc.file_name && frm.doc.file_name.split('.').splice(-1)[0]==='zip') {
frm.add_custom_button(__('Unzip'), function() {
frappe.call({


+ 16
- 0
frappe/core/doctype/file/file.py Ver ficheiro

@@ -937,6 +937,22 @@ def unzip_file(name):
files = file_obj.unzip()
return len(files)

@frappe.whitelist()
def optimize_saved_image(doc_name):
file_doc = frappe.get_doc('File', doc_name)
content = file_doc.get_content()
content_type = mimetypes.guess_type(file_doc.file_name)[0]

optimized_content = optimize_image(content, content_type)

file_path = get_files_path(is_private=file_doc.is_private)
file_path = os.path.join(file_path.encode('utf-8'), file_doc.file_name.encode('utf-8'))
with open(file_path, 'wb+') as f:
f.write(optimized_content)

file_doc.file_size = len(optimized_content)
file_doc.content_hash = get_content_hash(optimized_content)
file_doc.save()

@frappe.whitelist()
def get_attached_images(doctype, names):


Carregando…
Cancelar
Guardar