def execute():
    frappe.reload_doc('core', 'doctype', 'file_data')
    for name, file_name, file_url in frappe.db.sql(
            """select name, file_name, file_url from `tabFile`
			where file_name is not null"""):
        b = frappe.get_doc('File', name)
        old_file_name = b.file_name
        b.file_name = os.path.basename(old_file_name)
        if old_file_name.startswith('files/') or old_file_name.startswith(
                '/files/'):
            b.file_url = os.path.normpath('/' + old_file_name)
        else:
            b.file_url = os.path.normpath('/files/' + old_file_name)
        try:
            _file_name, content = get_file(name)
            b.content_hash = get_content_hash(content)
        except IOError:
            print('Warning: Error processing ', name)
            _file_name = old_file_name
            b.content_hash = None

        try:
            b.save()
        except frappe.DuplicateEntryError:
            frappe.delete_doc(b.doctype, b.name)
Example #2
0
    def insert_report_doc(self, dn=None):
        file_data = {}
        file_size = check_max_file_size(self.content)
        content_hash = get_content_hash(self.content)

        file_data.update({
            "doctype": "File Data",
            "attached_to_report_name": self.parent,
            "attached_to_doctype": self.dt,
            "attached_to_name": dn,
            "file_size": file_size,
            "content_hash": content_hash,
            'file_name': os.path.basename(self.rel_path),
            'file_url': os.sep + self.rel_path.replace('\\', '/'),
        })

        f = frappe.get_doc(file_data)
        try:
            f.insert(ignore_permissions=True)
            if self.ext == "jrxml":
                self.make_content_jrxml(f.name)

        except frappe.DuplicateEntryError:
            return frappe.get_doc("File Data", f.duplicate_entry)
        return f
	def insert_report_doc(self, dn=None):
		file_data = {}
		file_size = check_max_file_size(self.content)
		content_hash = get_content_hash(self.content)

		file_data.update({
			"doctype": "File",
			"attached_to_report_name": self.parent,
			"attached_to_doctype": self.dt,
			"attached_to_name": dn,
			"file_size": file_size,
			"content_hash": content_hash,
			'file_name': os.path.basename(self.rel_path),
			'file_url': os.sep + self.rel_path.replace('\\','/'),
		})

		f = frappe.get_doc(file_data)
		try:
			f.insert(ignore_permissions=True)
			if self.ext == "jrxml":
				self.make_content_jrxml(f.name)

		except frappe.DuplicateEntryError:
			return frappe.get_doc("File", f.duplicate_entry)
		return f
Example #4
0
	def generate_content_hash(self):
		if self.content_hash or not self.file_url:
			return

		if self.file_url.startswith("/files/"):
			try:
				with open(get_files_path(self.file_name.lstrip("/")), "r") as f:
					self.content_hash = get_content_hash(f.read())
			except IOError:
				frappe.msgprint(_("File {0} does not exist").format(self.file_url))
				raise
def rename_and_set_content_hash(files_path, unlinked_files, file_url):
	# rename this file
	old_filename = os.path.join(files_path, unlinked_files[file_url]["file"])
	new_filename = os.path.join(files_path, unlinked_files[file_url]["file_name"])

	if not os.path.exists(new_filename):
		os.rename(old_filename, new_filename)

	# set content hash if missing
	file_data_name = unlinked_files[file_url]["file"]
	if not frappe.db.get_value("File", file_data_name, "content_hash"):
		with open(new_filename, "r") as f:
			content_hash = get_content_hash(f.read())
			frappe.db.set_value("File", file_data_name, "content_hash", content_hash)
def execute():
	frappe.reload_doc('core', 'doctype', 'file_data')
	for name, file_name, file_url in frappe.db.sql(
			"""select name, file_name, file_url from `tabFile Data`
			where file_name is not null"""):
		b = frappe.get_doc('File Data', name)
		old_file_name = b.file_name
		b.file_name = os.path.basename(old_file_name)
		if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
			b.file_url = os.path.normpath('/' + old_file_name)
		else:
			b.file_url = os.path.normpath('/files/' + old_file_name)
		_file_name, content = get_file(name)
		b.content_hash = get_content_hash(content)
		b.save()
def rename_and_set_content_hash(files_path, unlinked_files, file_url):
    # rename this file
    old_filename = os.path.join(files_path, unlinked_files[file_url]["file"])
    new_filename = os.path.join(files_path,
                                unlinked_files[file_url]["file_name"])

    if not os.path.exists(new_filename):
        os.rename(old_filename, new_filename)

    # set content hash if missing
    file_data_name = unlinked_files[file_url]["file"]
    if not frappe.db.get_value("File", file_data_name, "content_hash"):
        with open(new_filename, "r") as f:
            content_hash = get_content_hash(f.read())
            frappe.db.set_value("File", file_data_name, "content_hash",
                                content_hash)
def execute():
    frappe.reload_doc('core', 'doctype', 'file_data')
    for name, file_name, file_url in frappe.db.sql(
            """select name, file_name, file_url from `tabFile Data`
			where file_name is not null"""):
        b = frappe.get_doc('File Data', name)
        old_file_name = b.file_name
        b.file_name = os.path.basename(old_file_name)
        if old_file_name.startswith('files/') or old_file_name.startswith(
                '/files/'):
            b.file_url = os.path.normpath('/' + old_file_name)
        else:
            b.file_url = os.path.normpath('/files/' + old_file_name)
        _file_name, content = get_file(name)
        b.content_hash = get_content_hash(content)
        b.save()
Example #9
0
    def get_fints_import_file_content(fints_import):
        """Get FinTS Import json file content as json.

        :param fints_import: fints_import doc
        :type fints_import: fints_import doc
        :return: Transaction from file as json object list
        """
        if fints_import.file_url:
            content = get_file(fints_import.file_url)[1]
            # Check content hash for file manipulations
            if fints_import.file_hash == get_content_hash(content):
                return frappe.json.loads(content, strict=False)
            else:
                raise ValueError('File hash does not match')
        else:
            return frappe.json.loads('[]')
	def insert_report_doc(self, dn=None):
		file_data = {}
		file_size = check_max_file_size(self.content)
		content_hash = get_content_hash(self.content)


		file_name = os.path.basename(self.rel_path)
		image = False

		file_url = os.sep + self.rel_path.replace('\\','/')
		try:
			Image.open(StringIO.StringIO(self.content)).verify()
			file_url = os.sep + "files" + os.sep + self.rel_path.replace('\\','/')
			image = True
		except Exception:
			pass


		file_data.update({
			"doctype": "File",
			"attached_to_report_name": self.parent,
			"attached_to_doctype": self.dt,
			"attached_to_name": dn,
			"file_size": file_size,
			"content_hash": content_hash,
			'file_name': file_name,
			'file_url': file_url,
		})

		f = frappe.get_doc(file_data)
		f.flags.ignore_file_validate = True
		if image:
			self.make_thumbnail(file_url, f, dn)

		under = "Home/Attachments"
		f.folder = under + os.path.dirname(file_url.replace("/files", ""))
		self.create_new_folder(file_url, dn)

		try:
			f.insert(ignore_permissions=True)
			if self.ext == "jrxml":
				self.make_content_jrxml(f.name)
		except frappe.DuplicateEntryError:
			return frappe.get_doc("File", f.duplicate_entry)
		return f
	def insert_report_doc(self, dn=None):
		file_data = {}
		file_size = check_max_file_size(self.content)
		content_hash = get_content_hash(self.content)


		file_name = os.path.basename(self.rel_path)
		image = False

		file_url = os.sep + self.rel_path.replace('\\','/')
		try:
			Image.open(StringIO.StringIO(self.content)).verify()
			file_url = os.sep + "files" + os.sep + self.rel_path.replace('\\','/')
			image = True
		except Exception:
			pass


		file_data.update({
			"doctype": "File",
			"attached_to_report_name": self.parent,
			"attached_to_doctype": self.dt,
			"attached_to_name": dn,
			"file_size": file_size,
			"content_hash": content_hash,
			'file_name': file_name,
			'file_url': file_url,
		})

		f = frappe.get_doc(file_data)
		f.flags.ignore_file_validate = True
		if image:
			self.make_thumbnail(file_url, f, dn)

		under = "Home/Attachments"
		f.folder = under + os.path.dirname(file_url.replace("/files", ""))
		self.create_new_folder(file_url, dn)

		try:
			f.insert(ignore_permissions=True)
			if self.ext == "jrxml":
				self.make_content_jrxml(f.name)
		except frappe.DuplicateEntryError:
			return frappe.get_doc("File", f.duplicate_entry)
		return f
Example #12
0
def execute():
	frappe.db.auto_commit_on_many_writes = True
	rename_replacing_files()
	for name, file_name, file_url in frappe.db.sql(
			"""select name, file_name, file_url from `tabFile`
			where ifnull(file_name, '')!='' and ifnull(content_hash, '')=''"""):
		b = frappe.get_doc('File', name)
		old_file_name = b.file_name
		b.file_name = os.path.basename(old_file_name)
		if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
			b.file_url = os.path.normpath('/' + old_file_name)
		else:
			b.file_url = os.path.normpath('/files/' + old_file_name)
		try:
			_file_name, content = get_file(name)
			b.content_hash = get_content_hash(content)
		except IOError:
			print 'Warning: Error processing ', name
			b.content_hash = None
		b.flags.ignore_duplicate_entry_error = True
		b.save()
	frappe.db.auto_commit_on_many_writes = False
Example #13
0
def execute():
	frappe.db.auto_commit_on_many_writes = True
	rename_replacing_files()
	for name, file_name, file_url in frappe.db.sql(
			"""select name, file_name, file_url from `tabFile`
			where ifnull(file_name, '')!='' and ifnull(content_hash, '')=''"""):
		b = frappe.get_doc('File', name)
		old_file_name = b.file_name
		b.file_name = os.path.basename(old_file_name)
		if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
			b.file_url = os.path.normpath('/' + old_file_name)
		else:
			b.file_url = os.path.normpath('/files/' + old_file_name)
		try:
			_file_name, content = get_file(name)
			b.content_hash = get_content_hash(content)
		except IOError:
			print('Warning: Error processing ', name)
			b.content_hash = None
		b.flags.ignore_duplicate_entry_error = True
		b.save()
	frappe.db.auto_commit_on_many_writes = False
Example #14
0
def execute():
	frappe.reload_doc('core', 'doctype', 'file_data')
	for name, file_name, file_url in frappe.db.sql(
			"""select name, file_name, file_url from `tabFile`
			where file_name is not null"""):
		b = frappe.get_doc('File', name)
		old_file_name = b.file_name
		b.file_name = os.path.basename(old_file_name)
		if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
			b.file_url = os.path.normpath('/' + old_file_name)
		else:
			b.file_url = os.path.normpath('/files/' + old_file_name)
		try:
			_file_name, content = get_file(name)
			b.content_hash = get_content_hash(content)
		except IOError:
			print('Warning: Error processing ', name)
			_file_name = old_file_name
			b.content_hash = None

		try:
			b.save()
		except frappe.DuplicateEntryError:
			frappe.delete_doc(b.doctype, b.name)