fix: Replace old upload with new FileUploader
This commit is contained in:
parent
6c576d9793
commit
93d6ec08eb
7 changed files with 83 additions and 140 deletions
|
|
@ -290,6 +290,8 @@ class File(NestedSet):
|
|||
|
||||
zip_path = frappe.get_site_path(self.file_url.strip('/'))
|
||||
base_url = os.path.dirname(self.file_url)
|
||||
|
||||
files = []
|
||||
with zipfile.ZipFile(zip_path) as zf:
|
||||
zf.extractall(os.path.dirname(zip_path))
|
||||
for info in zf.infolist():
|
||||
|
|
@ -308,8 +310,10 @@ class File(NestedSet):
|
|||
file_doc.attached_to_doctype = self.attached_to_doctype
|
||||
file_doc.attached_to_name = self.attached_to_name
|
||||
file_doc.save()
|
||||
files.append(file_doc)
|
||||
|
||||
frappe.delete_doc('File', self.name)
|
||||
return files
|
||||
|
||||
|
||||
def get_file_url(self):
|
||||
|
|
@ -888,7 +892,8 @@ def get_random_filename(extn=None, content_type=None):
|
|||
def unzip_file(name):
|
||||
'''Unzip the given file and make file records for each of the extracted files'''
|
||||
file_obj = frappe.get_doc('File', name)
|
||||
file_obj.unzip()
|
||||
files = file_obj.unzip()
|
||||
return len(files)
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
|
|
|
|||
|
|
@ -2132,10 +2132,11 @@ class extends Component {
|
|||
icon: "file",
|
||||
label: "File",
|
||||
onclick: ( ) => {
|
||||
const dialog = frappe.upload.make({
|
||||
args: { doctype: "Chat Room", docname: props.name },
|
||||
callback: (a, b, args) => {
|
||||
const { file_url, filename } = args
|
||||
new frappe.ui.FileUploader({
|
||||
doctype: "Chat Room",
|
||||
docname: props.name,
|
||||
on_success(file_doc) {
|
||||
const { file_url, filename } = file_doc
|
||||
frappe.chat.message.send(props.name, { path: file_url, name: filename }, "File")
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -146,25 +146,13 @@ frappe.ui.form.Attachments = Class.extend({
|
|||
this.dialog.$wrapper.remove();
|
||||
}
|
||||
|
||||
let flags = frappe.get_doc('Feature Flags');
|
||||
if (flags.new_upload_dialog) {
|
||||
new frappe.ui.FileUploader({
|
||||
doctype: this.frm.doctype,
|
||||
docname: this.frm.docname,
|
||||
on_success: (r) => {
|
||||
this.attachment_uploaded(r.message, r);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// make upload dialog
|
||||
this.dialog = frappe.ui.get_upload_dialog({
|
||||
"args": me.get_args(),
|
||||
"callback": function(attachment, r) { me.attachment_uploaded(attachment, r) },
|
||||
"max_width": me.frm.cscript ? me.frm.cscript.attachment_max_width : null,
|
||||
"max_height": me.frm.cscript ? me.frm.cscript.attachment_max_height : null
|
||||
});
|
||||
}
|
||||
|
||||
new frappe.ui.FileUploader({
|
||||
doctype: this.frm.doctype,
|
||||
docname: this.frm.docname,
|
||||
on_success: (file_doc) => {
|
||||
this.attachment_uploaded(file_doc);
|
||||
}
|
||||
});
|
||||
},
|
||||
get_args: function() {
|
||||
return {
|
||||
|
|
@ -173,7 +161,7 @@ frappe.ui.form.Attachments = Class.extend({
|
|||
docname: this.frm.docname,
|
||||
}
|
||||
},
|
||||
attachment_uploaded: function(attachment, r) {
|
||||
attachment_uploaded: function(attachment) {
|
||||
this.dialog && this.dialog.hide();
|
||||
this.update_attachment(attachment);
|
||||
this.frm.reload_docinfo();
|
||||
|
|
|
|||
|
|
@ -359,33 +359,25 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
var fields = this.dialog.fields_dict;
|
||||
var attach = $(fields.select_attachments.wrapper);
|
||||
|
||||
var me = this
|
||||
if (!me.attachments){
|
||||
me.attachments = []
|
||||
if (!this.attachments) {
|
||||
this.attachments = []
|
||||
}
|
||||
|
||||
var args = {
|
||||
args: {
|
||||
from_form: 1,
|
||||
folder:"Home/Attachments"
|
||||
},
|
||||
callback: function(attachment, r) { me.attachments.push(attachment); },
|
||||
max_width: null,
|
||||
max_height: null
|
||||
let args = {
|
||||
folder: 'Home/Attachments',
|
||||
on_success: attachment => this.attachments.push(attachment)
|
||||
};
|
||||
|
||||
if(me.frm) {
|
||||
if(this.frm) {
|
||||
args = {
|
||||
args: (me.frm.attachments.get_args
|
||||
? me.frm.attachments.get_args()
|
||||
: { from_form: 1,folder:"Home/Attachments" }),
|
||||
callback: function (attachment, r) {
|
||||
me.frm.attachments.attachment_uploaded(attachment, r)
|
||||
},
|
||||
max_width: me.frm.cscript ? me.frm.cscript.attachment_max_width : null,
|
||||
max_height: me.frm.cscript ? me.frm.cscript.attachment_max_height : null
|
||||
doctype: this.frm.doctype,
|
||||
docname: this.frm.docname,
|
||||
folder: 'Home/Attachments',
|
||||
on_success: attachment => {
|
||||
this.frm.attachments.attachment_uploaded(attachment);
|
||||
this.render_attach();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$("<h6 class='text-muted add-attachment' style='margin-top: 12px; cursor:pointer;'>"
|
||||
|
|
@ -393,11 +385,10 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
<p class='add-more-attachments'>\
|
||||
<a class='text-muted small'><i class='octicon octicon-plus' style='font-size: 12px'></i> "
|
||||
+__("Add Attachment")+"</a></p>").appendTo(attach.empty())
|
||||
attach.find(".add-more-attachments a").on('click',this,function() {
|
||||
me.upload = frappe.ui.get_upload_dialog(args);
|
||||
})
|
||||
me.render_attach()
|
||||
|
||||
attach
|
||||
.find(".add-more-attachments a")
|
||||
.on('click',() => new frappe.ui.FileUploader(args));
|
||||
this.render_attach();
|
||||
},
|
||||
render_attach:function(){
|
||||
var fields = this.dialog.fields_dict;
|
||||
|
|
|
|||
|
|
@ -103,25 +103,20 @@ frappe.views.FileView = class FileView extends frappe.views.ListView {
|
|||
{
|
||||
label: __('Import Zip'),
|
||||
action: () => {
|
||||
// make upload dialog
|
||||
frappe.ui.get_upload_dialog({
|
||||
args: {
|
||||
folder: this.current_folder,
|
||||
from_form: 1
|
||||
new frappe.ui.FileUploader({
|
||||
folder: this.current_folder,
|
||||
restrictions: {
|
||||
allowed_file_types: ['.zip']
|
||||
},
|
||||
callback: (attachment, r) => {
|
||||
frappe.call({
|
||||
method: 'frappe.core.doctype.file.file.unzip_file',
|
||||
args: {
|
||||
name: r.message.name,
|
||||
},
|
||||
callback: function (r) {
|
||||
if(r.exc) {
|
||||
frappe.msgprint(__('Error in uploading files' + r.exc));
|
||||
on_success: file => {
|
||||
frappe.show_alert(__('Unzipping files...'));
|
||||
frappe.call('frappe.core.doctype.file.file.unzip_file', { name: file.name })
|
||||
.then((r) => {
|
||||
if (r.message) {
|
||||
frappe.show_alert(__('Unzipped {0} files', [r.message]));
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -302,12 +297,9 @@ frappe.views.FileView = class FileView extends frappe.views.ListView {
|
|||
}
|
||||
|
||||
make_new_doc() {
|
||||
frappe.ui.get_upload_dialog({
|
||||
"args": {
|
||||
"folder": this.current_folder,
|
||||
"from_form": 1
|
||||
},
|
||||
callback:() => this.refresh()
|
||||
new frappe.ui.FileUploader({
|
||||
folder: this.current_folder,
|
||||
on_success: () => this.refresh()
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -326,21 +318,10 @@ frappe.views.FileView = class FileView extends frappe.views.ListView {
|
|||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
|
||||
let flags = frappe.get_doc('Feature Flags');
|
||||
if (flags.new_upload_dialog) {
|
||||
new frappe.ui.FileUploader({
|
||||
files: dataTransfer.files,
|
||||
folder: this.current_folder
|
||||
});
|
||||
} else {
|
||||
frappe.upload.make({
|
||||
files: dataTransfer.files,
|
||||
"args": {
|
||||
"folder": this.current_folder,
|
||||
"from_form": 1
|
||||
}
|
||||
});
|
||||
}
|
||||
new frappe.ui.FileUploader({
|
||||
files: dataTransfer.files,
|
||||
folder: this.current_folder
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -115,50 +115,39 @@ frappe.views.InteractionComposer = class InteractionComposer {
|
|||
}
|
||||
|
||||
setup_attach() {
|
||||
let fields = this.dialog.fields_dict;
|
||||
let attach = $(fields.select_attachments.wrapper);
|
||||
var fields = this.dialog.fields_dict;
|
||||
var attach = $(fields.select_attachments.wrapper);
|
||||
|
||||
let me = this;
|
||||
if (!me.attachments){
|
||||
me.attachments = [];
|
||||
if (!this.attachments) {
|
||||
this.attachments = []
|
||||
}
|
||||
|
||||
let args = {
|
||||
args: {
|
||||
from_form: 1,
|
||||
folder:"Home/Attachments"
|
||||
},
|
||||
callback: function(attachment){
|
||||
me.attachments.push(attachment);
|
||||
},
|
||||
max_width: null,
|
||||
max_height: null
|
||||
folder: 'Home/Attachments',
|
||||
on_success: attachment => this.attachments.push(attachment)
|
||||
};
|
||||
|
||||
if(me.frm) {
|
||||
if (this.frm) {
|
||||
args = {
|
||||
args: (me.frm.attachments.get_args
|
||||
? me.frm.attachments.get_args()
|
||||
: { from_form: 1,folder:"Home/Attachments" }),
|
||||
callback: function(attachment, r){
|
||||
me.frm.attachments.attachment_uploaded(attachment, r);
|
||||
},
|
||||
max_width: me.frm.cscript ? me.frm.cscript.attachment_max_width : null,
|
||||
max_height: me.frm.cscript ? me.frm.cscript.attachment_max_height : null
|
||||
};
|
||||
|
||||
doctype: this.frm.doctype,
|
||||
docname: this.frm.docname,
|
||||
folder: 'Home/Attachments',
|
||||
on_success: attachment => {
|
||||
this.frm.attachments.attachment_uploaded(attachment);
|
||||
this.render_attach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$("<h6 class='text-muted add-attachment' style='margin-top: 12px; cursor:pointer;'>"
|
||||
+__("Select Attachments")+"</h6><div class='attach-list'></div>\
|
||||
<p class='add-more-attachments'>\
|
||||
<a class='text-muted small'><i class='octicon octicon-plus' style='font-size: 12px'></i> "
|
||||
+__("Add Attachment")+"</a></p>").appendTo(attach.empty());
|
||||
attach.find(".add-more-attachments a").on('click',this,function() {
|
||||
me.upload = frappe.ui.get_upload_dialog(args);
|
||||
});
|
||||
me.render_attach();
|
||||
|
||||
+__("Add Attachment")+"</a></p>").appendTo(attach.empty())
|
||||
attach
|
||||
.find(".add-more-attachments a")
|
||||
.on('click',() => new frappe.ui.FileUploader(args));
|
||||
this.render_attach();
|
||||
}
|
||||
|
||||
render_attach(){
|
||||
|
|
|
|||
|
|
@ -142,26 +142,14 @@ _f.Frm.prototype.setup_drag_drop = function() {
|
|||
throw "attach error";
|
||||
}
|
||||
|
||||
let flags = frappe.get_doc('Feature Flags');
|
||||
|
||||
if (flags.new_upload_dialog) {
|
||||
new frappe.ui.FileUploader({
|
||||
doctype: me.doctype,
|
||||
docname: me.docname,
|
||||
files: dataTransfer.files,
|
||||
on_success(r) {
|
||||
me.attachments.attachment_uploaded(r.message, r);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
frappe.upload.make({
|
||||
args: me.attachments.get_args(),
|
||||
files: dataTransfer.files,
|
||||
callback: function(attachment, r) {
|
||||
me.attachments.attachment_uploaded(attachment, r);
|
||||
}
|
||||
});
|
||||
}
|
||||
new frappe.ui.FileUploader({
|
||||
doctype: me.doctype,
|
||||
docname: me.docname,
|
||||
files: dataTransfer.files,
|
||||
on_success(file_doc) {
|
||||
me.attachments.attachment_uploaded(file_doc);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue