Ejemplo n.º 1
0
def zip_attachments(document):
    zip_count = 1
    zip_size = 0
    document = json.loads(document)
    document2 = frappe._dict(document)

    fname = get_file_name(document2.name + " (zip 1).zip", random_string(7))

    import zipfile
    docZip = zipfile.ZipFile(fname, "w", zipfile.ZIP_DEFLATED)

    for file_url in frappe.db.sql(
            """select file_url, is_private from `tabFile` where attached_to_doctype = %(doctype)s and attached_to_name = %(docname)s""",
        {
            'doctype': document2.doctype,
            'docname': document2.name
        },
            as_dict=True):
        frappe.msgprint("Adding " + file_url.file_url)

        if file_url.file_url.startswith("/private/files/"):
            path = get_files_path(*file_url.file_url.split(
                "/private/files/", 1)[1].split("/"),
                                  is_private=1)

        elif file_url.file_url.startswith("/files/"):
            path = get_files_path(
                *file_url.file_url.split("/files/", 1)[1].split("/"))

        path = encode(path)
        if zip_size + os.path.getsize(path) > 10000000:
            zip_count = zip_count + 1
            zip_size = 0
            docZip.close()
            with open(encode(fname), 'r') as f:
                content = f.read()

            content = base64.b64encode(content)

            save_file(fname, content, document2.doctype, document2.name,
                      "Home/Attachments", 1)
            fname = get_file_name(
                document2.name + " (zip " + str(zip_count) + ").zip",
                random_string(7))
            docZip = zipfile.ZipFile(fname, "w", zipfile.ZIP_DEFLATED)
        docZip.write(path, os.path.basename(path))
        zip_size = zip_size + docZip.getinfo(
            os.path.basename(path)).compress_size

    docZip.close()
    with open(encode(fname), 'r') as f:
        content = f.read()

    content = base64.b64encode(content)

    save_file(fname, content, document2.doctype, document2.name,
              "Home/Attachments", 1)
Ejemplo n.º 2
0
def rename_replacing_files():
    replaced_files = get_replaced_files()
    if len(replaced_files):
        missing_files = [v[0] for v in replaced_files]
        with open(get_site_path('missing_files.txt'), 'w') as f:
            f.write(('\n'.join(missing_files) + '\n').encode('utf-8'))

    for file_name, file_datas in replaced_files:
        print 'processing ' + file_name
        content_hash = frappe.db.get_value('File', file_datas[0],
                                           'content_hash')
        if not content_hash:
            continue
        new_file_name = get_file_name(file_name, content_hash)
        if os.path.exists(get_files_path(new_file_name)):
            continue
            print 'skipping ' + file_name
        try:
            os.rename(get_files_path(file_name), get_files_path(new_file_name))
        except OSError:
            print 'Error renaming ', file_name
        for name in file_datas:
            f = frappe.get_doc('File', name)
            f.file_name = new_file_name
            f.file_url = '/files/' + new_file_name
            f.save()
Ejemplo n.º 3
0
def rename_replacing_files():
	replaced_files = get_replaced_files()
	if len(replaced_files):
		missing_files = [v[0] for v in replaced_files]
		with open(get_site_path('missing_files.txt'), 'w') as f:
			f.write(('\n'.join(missing_files) + '\n').encode('utf-8'))

	for file_name, file_datas in replaced_files:
		print 'processing ' + file_name
		content_hash = frappe.db.get_value('File', file_datas[0], 'content_hash')
		if not content_hash:
			continue
		new_file_name = get_file_name(file_name, content_hash)
		if os.path.exists(get_files_path(new_file_name)):
			continue
			print 'skipping ' + file_name
		try:
			os.rename(get_files_path(file_name), get_files_path(new_file_name))
		except OSError:
			print 'Error renaming ', file_name
		for name in file_datas:
			f = frappe.get_doc('File', name)
			f.file_name = new_file_name
			f.file_url = '/files/' + new_file_name
			f.save()
Ejemplo n.º 4
0
def zip_attachments(document):
	zip_count = 1
	zip_size = 0
	document = json.loads(document)
	document2 = frappe._dict(document)

	
	fname = get_file_name(document2.name + " (zip 1).zip", random_string(7))
	
	import zipfile
	docZip = zipfile.ZipFile(fname,"w", zipfile.ZIP_DEFLATED)
	
	
	for file_url in frappe.db.sql("""select file_url, is_private from `tabFile` where attached_to_doctype = %(doctype)s and attached_to_name = %(docname)s""", {'doctype': document2.doctype, 'docname': document2.name}, as_dict=True ):
		frappe.msgprint("Adding " + file_url.file_url)
		
		if file_url.file_url.startswith("/private/files/"):
			path = get_files_path(*file_url.file_url.split("/private/files/", 1)[1].split("/"), is_private=1)

		elif file_url.file_url.startswith("/files/"):
			path = get_files_path(*file_url.file_url.split("/files/", 1)[1].split("/"))
		
		path = encode(path)
		if zip_size + os.path.getsize(path) > 10000000:
			zip_count = zip_count + 1
			zip_size = 0
			docZip.close()
			with open(encode(fname), 'r') as f:
				content = f.read()
			
			content = base64.b64encode(content)
				
			save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1)
			fname = get_file_name(document2.name + " (zip " + str(zip_count) + ").zip", random_string(7))
			docZip = zipfile.ZipFile(fname,"w", zipfile.ZIP_DEFLATED)
		docZip.write(path, os.path.basename(path))
		zip_size  = zip_size + docZip.getinfo(os.path.basename(path)).compress_size
		

	docZip.close()
	with open(encode(fname), 'r') as f:
		content = f.read()
	
	content = base64.b64encode(content)
		
	save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1)