def save_file(fname, content, dt, dn, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") content = base64.b64decode(content) file_size = check_max_file_size(content) content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = get_file_data_from_hash(content_hash) if not file_data: file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) file_data.update({ "doctype": "File Data", "attached_to_doctype": dt, "attached_to_name": dn, "file_size": file_size, "content_hash": content_hash, }) f = frappe.get_doc(file_data) f.ignore_permissions = True try: f.insert() except frappe.DuplicateEntryError: return frappe.get_doc("File Data", f.duplicate_entry) return f
def save_file(fname, content, ref_no, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") if "," in content: content = content.split(",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) file_data = get_file_data_from_hash(content_hash) if not file_data: method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) file_data.update({ "doctype": "Compose", "file_size": file_size, "ref_no": ref_no }) f = frappe.get_doc(file_data) f.flags.ignore_permissions = True try: f.insert() except frappe.DuplicateEntryError: return frappe.get_doc("Compose", f.duplicate_entry) return f
def save_file(fname, content, dt, dn, folder=None, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") if "," in content: content = content.split(",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) file_data = get_file_data_from_hash(content_hash) if not file_data: method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) file_data.update({ "doctype": "File", "attached_to_doctype": dt, "attached_to_name": dn, "folder": folder, "file_size": file_size, "content_hash": content_hash, }) f = frappe.get_doc(file_data) f.flags.ignore_permissions = True try: f.insert(); except frappe.DuplicateEntryError: return frappe.get_doc("File", f.duplicate_entry) return f
def save_file(self, content=None, decode=False): file_exists = False self.content = content if decode: if isinstance(content, text_type): self.content = content.encode("utf-8") if b"," in self.content: self.content = self.content.split(b",")[1] self.content = base64.b64decode(self.content) if not self.is_private: self.is_private = 0 self.file_size = self.check_max_file_size() self.content_hash = get_content_hash(self.content) self.content_type = mimetypes.guess_type(self.file_name)[0] _file = frappe.get_value("File", {"content_hash": self.content_hash}, ["file_url"]) if _file: self.file_url = _file file_exists = True if not file_exists: if os.path.exists(encode(get_files_path(self.file_name))): self.file_name = get_file_name(self.file_name, self.content_hash[-6:]) call_hook_method("before_write_file", file_size=self.file_size) write_file_method = get_hook_method('write_file') if write_file_method: return write_file_method(self) return self.save_file_on_filesystem()
def save_file(self, content=None, decode=False, ignore_existing_file_check=False): file_exists = False self.content = content if decode: if isinstance(content, text_type): self.content = content.encode("utf-8") if b"," in self.content: self.content = self.content.split(b",")[1] self.content = base64.b64decode(self.content) if not self.is_private: self.is_private = 0 self.content_type = mimetypes.guess_type(self.file_name)[0] self.file_size = self.check_max_file_size() if (self.content_type and "image" in self.content_type and frappe.get_system_settings( "strip_exif_metadata_from_uploaded_images")): self.content = strip_exif_data(self.content, self.content_type) self.content_hash = get_content_hash(self.content) duplicate_file = None # check if a file exists with the same content hash and is also in the same folder (public or private) if not ignore_existing_file_check: duplicate_file = frappe.get_value("File", { "content_hash": self.content_hash, "is_private": self.is_private }, ["file_url", "name"], as_dict=True) if duplicate_file: file_doc = frappe.get_cached_doc('File', duplicate_file.name) if file_doc.exists_on_disk(): self.file_url = duplicate_file.file_url file_exists = True if os.path.exists( encode( get_files_path(self.file_name, is_private=self.is_private))): self.file_name = get_file_name(self.file_name, self.content_hash[-6:]) if not file_exists: call_hook_method("before_write_file", file_size=self.file_size) write_file_method = get_hook_method('write_file') if write_file_method: return write_file_method(self) return self.save_file_on_filesystem()
def save_file(fname, content, dt, dn, folder=None, decode=False, is_private=0, df=None): if decode: if isinstance(content, text_type): content = content.encode("utf-8") if b"," in content: content = content.split(b",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) file_data = get_file_data_from_hash(content_hash, is_private=is_private) if not file_data: call_hook_method("before_write_file", file_size=file_size) write_file_method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = write_file_method(fname, content, content_type=content_type, is_private=is_private) file_data = copy(file_data) file_data.update({ "doctype": "File", "attached_to_doctype": dt, "attached_to_name": dn, "attached_to_field": df, "folder": folder, "file_size": file_size, "content_hash": content_hash, "is_private": is_private }) f = frappe.get_doc(file_data) f.flags.ignore_permissions = True try: f.insert() except frappe.DuplicateEntryError: return frappe.get_doc("File", f.duplicate_entry) return f
def save_file(fname, content, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") if "," in content: content = content.split(",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) if file_size.get('exe'): return {"msg": file_size.get('exe'), "fname": fname} content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) return {"msg":"File Uploaded Successfully", "fname": fname}
def save_file(fname, content, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") if "," in content: content = content.split(",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) if file_size.get('exe'): return {"msg": file_size.get('exe'), "fname": fname} content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) return {"msg": "File Uploaded Successfully", "fname": fname}
def save_file(fname, content, decode=False): if decode: if isinstance(content, unicode): content = content.encode("utf-8") if "," in content: content = content.split(",")[1] content = base64.b64decode(content) file_size = check_max_file_size(content) if file_size.get('exe'): return {"msg": file_size.get('exe'), "fname": fname} content_hash = get_content_hash(content) content_type = mimetypes.guess_type(fname)[0] fname = get_file_name(fname, content_hash[-6:]) # frappe.errprint([file_size, content_hash, content_type, fname]) # file_data = get_file_data_from_hash(content_hash) # frappe.errprint(file_data) # if not file_data: method = get_hook_method('write_file', fallback=save_file_on_filesystem) file_data = method(fname, content, content_type=content_type) file_data = copy(file_data) return {"msg":"Attachment Successful", "fname": fname}
def delete_file_data_content(doc): method = get_hook_method('delete_file_data_content', fallback=delete_file_from_filesystem) method(doc)
def delete_file_data_content(self, only_thumbnail=False): method = get_hook_method('delete_file_data_content') if method: method(self, only_thumbnail=only_thumbnail) else: self.delete_file_from_filesystem(only_thumbnail=only_thumbnail)
def delete_file_data_content(doc, only_thumbnail=False): method = get_hook_method('delete_file_data_content', fallback=delete_file_from_filesystem) method(doc, only_thumbnail=only_thumbnail)
def send_one(email, smtpserver=None, auto_commit=True, now=False): """Send Email Queue with given smtpserver""" email = frappe.db.sql( """select name, status, communication, message, sender, reference_doctype, reference_name, unsubscribe_param, unsubscribe_method, expose_recipients, show_as_cc, add_unsubscribe_link, attachments, retry from `tabEmail Queue` where name=%s for update""", email, as_dict=True, ) if len(email): email = email[0] else: return recipients_list = frappe.db.sql( """select name, recipient, status from `tabEmail Queue Recipient` where parent=%s""", email.name, as_dict=1, ) if frappe.are_emails_muted(): frappe.msgprint(_("Emails are muted")) return if cint(frappe.defaults.get_defaults().get("hold_queue")) == 1: return if email.status not in ("Not Sent", "Partially Sent"): # rollback to release lock and return frappe.db.rollback() return frappe.db.sql( """update `tabEmail Queue` set status='Sending', modified=%s where name=%s""", (now_datetime(), email.name), auto_commit=auto_commit, ) if email.communication: frappe.get_doc( "Communication", email.communication).set_delivery_status(commit=auto_commit) email_sent_to_any_recipient = None try: message = None if not frappe.flags.in_test: if not smtpserver: smtpserver = SMTPServer() # to avoid always using default email account for outgoing if getattr(frappe.local, "outgoing_email_account", None): frappe.local.outgoing_email_account = {} smtpserver.setup_email_account(email.reference_doctype, sender=email.sender) for recipient in recipients_list: if recipient.status != "Not Sent": continue message = prepare_message(email, recipient.recipient, recipients_list) if not frappe.flags.in_test: method = get_hook_method("override_email_send") if method: queue = frappe.get_doc("Email Queue", email.name) method(queue, email.sender, recipient.recipient, message) return else: smtpserver.sess.sendmail(email.sender, recipient.recipient, message) recipient.status = "Sent" frappe.db.sql( """update `tabEmail Queue Recipient` set status='Sent', modified=%s where name=%s""", (now_datetime(), recipient.name), auto_commit=auto_commit, ) email_sent_to_any_recipient = any("Sent" == s.status for s in recipients_list) # if all are sent set status if email_sent_to_any_recipient: frappe.db.sql( """update `tabEmail Queue` set status='Sent', modified=%s where name=%s""", (now_datetime(), email.name), auto_commit=auto_commit, ) else: frappe.db.sql( """update `tabEmail Queue` set status='Error', error=%s where name=%s""", ("No recipients to send to", email.name), auto_commit=auto_commit, ) if frappe.flags.in_test: frappe.flags.sent_mail = message return if email.communication: frappe.get_doc( "Communication", email.communication).set_delivery_status(commit=auto_commit) if smtpserver.append_emails_to_sent_folder and email_sent_to_any_recipient: smtpserver.email_account.append_email_to_sent_folder(message) except ( smtplib.SMTPServerDisconnected, smtplib.SMTPConnectError, smtplib.SMTPHeloError, smtplib.SMTPAuthenticationError, smtplib.SMTPRecipientsRefused, JobTimeoutException, ): # bad connection/timeout, retry later if email_sent_to_any_recipient: frappe.db.sql( """update `tabEmail Queue` set status='Partially Sent', modified=%s where name=%s""", (now_datetime(), email.name), auto_commit=auto_commit, ) else: frappe.db.sql( """update `tabEmail Queue` set status='Not Sent', modified=%s where name=%s""", (now_datetime(), email.name), auto_commit=auto_commit, ) if email.communication: frappe.get_doc( "Communication", email.communication).set_delivery_status(commit=auto_commit) # no need to attempt further return except Exception as e: frappe.db.rollback() if email.retry < 3: frappe.db.sql( """update `tabEmail Queue` set status='Not Sent', modified=%s, retry=retry+1 where name=%s""", (now_datetime(), email.name), auto_commit=auto_commit, ) else: if email_sent_to_any_recipient: frappe.db.sql( """update `tabEmail Queue` set status='Partially Errored', error=%s where name=%s""", (text_type(e), email.name), auto_commit=auto_commit, ) else: frappe.db.sql( """update `tabEmail Queue` set status='Error', error=%s where name=%s""", (text_type(e), email.name), auto_commit=auto_commit, ) if email.communication: frappe.get_doc( "Communication", email.communication).set_delivery_status(commit=auto_commit) if now: print(frappe.get_traceback()) raise e else: # log to Error Log frappe.log_error("frappe.email.queue.flush")