Exemplo n.º 1
0
    def get_attachment(self, part):
        #charset = self.get_charset(part)
        fcontent = part.get_payload(decode=True)

        if fcontent:
            content_type = part.get_content_type()
            fname = part.get_filename()
            if fname:
                try:
                    fname = fname.replace('\n', ' ').replace('\r', '')
                    fname = cstr(decode_header(fname)[0][0])
                except:
                    fname = get_random_filename(content_type=content_type)
            else:
                fname = get_random_filename(content_type=content_type)

            self.attachments.append({
                'content_type': content_type,
                'fname': fname,
                'fcontent': fcontent,
            })

            cid = (part.get("Content-Id") or "").strip("><")
            if cid:
                self.cid_map[fname] = cid
Exemplo n.º 2
0
	def get_attachment(self, part):
		#charset = self.get_charset(part)
		fcontent = part.get_payload(decode=True)

		if fcontent:
			content_type = part.get_content_type()
			fname = part.get_filename()
			if fname:
				try:
					fname = fname.replace('\n', ' ').replace('\r', '')
					fname = cstr(decode_header(fname)[0][0])
				except:
					fname = get_random_filename(content_type=content_type)
			else:
				fname = get_random_filename(content_type=content_type)

			self.attachments.append({
				'content_type': content_type,
				'fname': fname,
				'fcontent': fcontent,
			})

			cid = (part.get("Content-Id") or "").strip("><")
			if cid:
				self.cid_map[fname] = cid
Exemplo n.º 3
0
def get_web_image(file_url):
    # download
    file_url = frappe.utils.get_url(file_url)
    r = requests.get(file_url, stream=True)
    try:
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if "404" in e.args[0]:
            frappe.msgprint(_("File '{0}' not found").format(file_url))
        else:
            frappe.msgprint(
                _("Unable to read file format for {0}").format(file_url))
        raise

    image = Image.open(StringIO(r.content))

    try:
        filename, extn = file_url.rsplit("/", 1)[1].rsplit(".", 1)
    except ValueError:
        # the case when the file url doesn't have filename or extension
        # but is fetched due to a query string. example: https://encrypted-tbn3.gstatic.com/images?q=something
        filename = get_random_filename()
        extn = None

    extn = get_extension(filename, extn, r.content)
    filename = "/files/" + strip(unquote(filename))

    return image, filename, extn
Exemplo n.º 4
0
def get_web_image(file_url):
	# download
	file_url = frappe.utils.get_url(file_url)
	r = requests.get(file_url, stream=True)
	try:
		r.raise_for_status()
	except requests.exceptions.HTTPError as e:
		if "404" in e.args[0]:
			frappe.msgprint(_("File '{0}' not found").format(file_url))
		else:
			frappe.msgprint(_("Unable to read file format for {0}").format(file_url))
		raise

	image = Image.open(StringIO(r.content))

	try:
		filename, extn = file_url.rsplit("/", 1)[1].rsplit(".", 1)
	except ValueError:
		# the case when the file url doesn't have filename or extension
		# but is fetched due to a query string. example: https://encrypted-tbn3.gstatic.com/images?q=something
		filename = get_random_filename()
		extn = None

	extn = get_extension(filename, extn, r.content)
	filename = "/files/" + strip(unquote(filename))

	return image, filename, extn
Exemplo n.º 5
0
def write_to_local(content):
    import os
    from frappe.utils.file_manager import get_random_filename
    public_files = frappe.get_site_path('public', 'files')
    fname = get_random_filename(extn=".pdf")
    with open(os.path.join(public_files, fname), "w") as f:
        f.write(content)
Exemplo n.º 6
0
    def get_attachment(self, part, charset):
        fcontent = part.get_payload(decode=True)

        if fcontent:
            content_type = part.get_content_type()
            fname = part.get_filename()
            if fname:
                try:
                    fname = cstr(decode_header(fname)[0][0])
                except:
                    fname = get_random_filename(content_type=content_type)
            else:
                fname = get_random_filename(content_type=content_type)

            self.attachments.append({"content_type": content_type, "fname": fname, "fcontent": fcontent})

            self.cid_map[fname] = part.get("Content-Id").strip("><")
Exemplo n.º 7
0
	def get_attachment(self, part, charset):
		fcontent = part.get_payload(decode=True)

		if fcontent:
			content_type = part.get_content_type()
			fname = part.get_filename()
			if fname:
				try:
					fname = cstr(decode_header(fname)[0][0])
				except:
					fname = get_random_filename(content_type=content_type)
			else:
				fname = get_random_filename(content_type=content_type)

			self.attachments.append({
				'content_type': content_type,
				'fname': fname,
				'fcontent': fcontent,
			})
Exemplo n.º 8
0
    def get_attachment(self, part, charset):
        fcontent = part.get_payload(decode=True)

        if fcontent:
            content_type = part.get_content_type()
            fname = part.get_filename()
            if fname:
                try:
                    fname = cstr(decode_header(fname)[0][0])
                except:
                    fname = get_random_filename(content_type=content_type)
            else:
                fname = get_random_filename(content_type=content_type)

            self.attachments.append({
                'content_type': content_type,
                'fname': fname,
                'fcontent': fcontent,
            })
Exemplo n.º 9
0
def get_web_image(file_url):
    # downlaod
    file_url = frappe.utils.get_url(file_url)
    r = requests.get(file_url, stream=True)
    try:
        r.raise_for_status()
    except requests.exceptions.HTTPError, e:
        if "404" in e.args[0]:
            frappe.msgprint(_("File '{0}' not found").format(file_url))
        else:
            frappe.msgprint(
                "Unable to read file format for {0}".format(file_url))

        raise

    image = Image.open(StringIO.StringIO(r.content))

    try:
        filename, extn = file_url.rsplit("/", 1)[1].rsplit(".", 1)
    except ValueError:
        # the case when the file url doesn't have filename or extension
        # but is fetched due to a query string. example: https://encrypted-tbn3.gstatic.com/images?q=something
        filename = get_random_filename()
        extn = None

    extn = get_extension(filename, extn, r.content)
    filename = "/files/" + strip(urllib.unquote(filename))

    return image, filename, extn
Exemplo n.º 10
0
		r.raise_for_status()
	except requests.exceptions.HTTPError, e:
		if "404" in e.args[0]:
			frappe.msgprint(_("File '{0}' not found").format(file_url))
		else:
			frappe.msgprint("Unable to read file format for {0}".format(file_url))
		raise

	image = Image.open(StringIO.StringIO(r.content))

	try:
		filename, extn = file_url.rsplit("/", 1)[1].rsplit(".", 1)
	except ValueError:
		# the case when the file url doesn't have filename or extension
		# but is fetched due to a query string. example: https://encrypted-tbn3.gstatic.com/images?q=something
		filename = get_random_filename()
		extn = None

	extn = get_extension(filename, extn, r.content)
	filename = "/files/" + strip(urllib.unquote(filename))

	return image, filename, extn

def check_file_permission(file_url):
	for file in frappe.get_all("File", filters={"file_url": file_url, "is_private": 1}, fields=["name", "attached_to_doctype", "attached_to_name"]):

		if (frappe.has_permission("File", ptype="read", doc=file.name)
			or frappe.has_permission(file.attached_to_doctype, ptype="read", doc=file.attached_to_name)):
			return True

	raise frappe.PermissionError