venkata akhil
1 year ago
11 changed files with 463 additions and 27 deletions
@ -0,0 +1,148 @@ |
|||
import frappe |
|||
import os |
|||
|
|||
@frappe.whitelist() |
|||
def check_img_ext(path): |
|||
if path: |
|||
ext = path.split()[-1] |
|||
if ext.lower().endswith('.jpg') or ext.lower().endswith('.jpeg'): |
|||
return True |
|||
else: |
|||
return False |
|||
else: |
|||
return False |
|||
|
|||
|
|||
@frappe.whitelist() |
|||
def check_png_ext(path): |
|||
if path: |
|||
ext = path.split()[-1] |
|||
if ext.lower().endswith('.png'): |
|||
return True |
|||
else: |
|||
return False |
|||
else: |
|||
return False |
|||
|
|||
|
|||
@frappe.whitelist() |
|||
def check_zip_ext(path): |
|||
if path: |
|||
ext = path.split()[-1] |
|||
if ext.lower().endswith('.zip'): |
|||
return True |
|||
else: |
|||
return False |
|||
else: |
|||
return False |
|||
|
|||
|
|||
@frappe.whitelist() |
|||
def check_pdf_ext(path): |
|||
if path: |
|||
ext = path.split()[-1] |
|||
if ext.lower().endswith('.pdf'): |
|||
return True |
|||
else: |
|||
return False |
|||
else: |
|||
return False |
|||
|
|||
|
|||
@frappe.whitelist() |
|||
def check_mp4_ext(path): |
|||
if path: |
|||
ext = path.split()[-1] |
|||
if ext.lower().endswith('.mp4'): |
|||
return True |
|||
else: |
|||
return False |
|||
|
|||
|
|||
def details_of_image(image_file): |
|||
try: |
|||
# Image dimension validation |
|||
import os |
|||
from PIL import Image |
|||
if image_file.startswith('/private/'): |
|||
filepath = get_absolute_path(image_file, True) |
|||
else: |
|||
filepath = get_absolute_path(image_file) |
|||
img = Image.open(filepath) |
|||
width = img.width |
|||
height = img.height |
|||
size = os.stat(filepath) |
|||
size=size[6]/1024 |
|||
return width, height,size |
|||
except Exception as e: |
|||
raise e |
|||
|
|||
def get_file_size(filepath): |
|||
if filepath.startswith('/private/'): |
|||
file_dir = get_absolute_path(filepath, True) |
|||
else: |
|||
file_dir = get_absolute_path(filepath) |
|||
|
|||
size_in_bytes = os.path.getsize(file_dir) |
|||
size_in_megabytes = size_in_bytes / (1024 * 1024) |
|||
return size_in_megabytes |
|||
|
|||
|
|||
def get_image_size(filepath): |
|||
if filepath.startswith('/private/'): |
|||
file_dir = get_absolute_path(filepath, True) |
|||
else: |
|||
file_dir = get_absolute_path(filepath) |
|||
|
|||
size_in_bytes = os.path.getsize(file_dir) |
|||
size_in_kilobytes = size_in_bytes / 1024 |
|||
return size_in_kilobytes |
|||
|
|||
|
|||
def get_absolute_path(file_name, is_private=False): |
|||
try: |
|||
if file_name.startswith('/files/'): |
|||
file_name = file_name[7:] |
|||
else: |
|||
file_name = file_name[15:] |
|||
return frappe.utils.get_bench_path() + "/sites/" + frappe.utils.get_path('private' if is_private else 'public', |
|||
'files', file_name)[2:] |
|||
except Exception as e: |
|||
frappe.log_error("get_absolute_path", frappe.get_traceback()) |
|||
|
|||
|
|||
|
|||
def checking_image(file_url, attached_to_doctype, attached_to_field, docname, is_child=None): |
|||
try: |
|||
import time |
|||
time.sleep(1) |
|||
if docname.startswith('new'): |
|||
image_name = frappe.db.get_list("File", fields={"name"}, filters={"file_url": file_url, |
|||
"attached_to_doctype": attached_to_doctype, |
|||
"attached_to_field": attached_to_field, |
|||
"attached_to_name": docname}) |
|||
|
|||
else: |
|||
image_name = frappe.db.get_list("File", fields={"name"}, filters={"file_url": file_url, |
|||
"attached_to_doctype": attached_to_doctype, |
|||
"attached_to_field": attached_to_field}) |
|||
|
|||
image_name = frappe.db.sql(f"""select name from `tabFile` where file_url='{file_url}' and attached_to_doctype='{attached_to_doctype}' and |
|||
attached_to_field='{attached_to_field}'""",as_dict=1) |
|||
# frappe.msgprint("data-"+str(file_url)+"-"+str(attached_to_doctype)+"-"+str(attached_to_field)+"-"+str(docname)) |
|||
if not attached_to_doctype == "Symbol Scanner" and is_child != 1: |
|||
frappe.db.sql( |
|||
'''UPDATE `tab{0}` SET {2} = '' where name = "{1}"'''.format(attached_to_doctype, docname, |
|||
attached_to_field)) |
|||
frappe.db.commit() |
|||
if attached_to_doctype == "Symbol Scanner": |
|||
frappe.db.set_value( |
|||
"Symbol Scanner", "Symbol Scanner", attached_to_field, "") |
|||
|
|||
if len(image_name) > 0: |
|||
frappe.delete_doc('File', image_name[0]['name']) |
|||
return True |
|||
|
|||
except Exception as e: |
|||
frappe.log_error("Checking images", frappe.get_traceback()) |
|||
|
@ -1,8 +1,42 @@ |
|||
# Copyright (c) 2023, Hard n Soft Technologies Pvt Ltd and contributors |
|||
# For license information, please see license.txt |
|||
|
|||
# import frappe |
|||
import frappe |
|||
from frappe.model.document import Document |
|||
|
|||
class FeatureFinderMaster(Document): |
|||
pass |
|||
|
|||
|
|||
@frappe.whitelist() |
|||
def file_validation(image, name, value=None): |
|||
from smart_service.apis.utils import check_png_ext, check_img_ext,check_pdf_ext,details_of_image |
|||
if image: |
|||
res = check_pdf_ext(image) |
|||
if res == True: |
|||
pass |
|||
# (width, height, size) = details_of_image(image) |
|||
|
|||
# if width > 640 or height > 360 and image: |
|||
# val = delete_feature_image(image) |
|||
# if val == True: |
|||
# return (False, 2) |
|||
# else: |
|||
# return True |
|||
|
|||
else: |
|||
val = delete_file(image, 'Feature Finder Master', |
|||
"feature_pdf", name) |
|||
if res == False: |
|||
ret = res |
|||
return ret, 1 |
|||
|
|||
|
|||
def delete_file(file_url, attached_to_doctype, attached_to_field, docname, is_child=None): |
|||
image_name = frappe.db.get_list("File", fields={"name"}, filters={"file_url": file_url, |
|||
"attached_to_doctype": attached_to_doctype, |
|||
"attached_to_field": attached_to_field, |
|||
"attached_to_name": docname}) |
|||
if len(image_name) > 0: |
|||
frappe.delete_doc('File', image_name[0]['name']) |
|||
return True |
|||
|
Loading…
Reference in new issue