async def emailmessage( sender: str, password: str, subject: str, context: str, port: str = 587, host: str = "smtp.gmail.com", htmlfile: str = 'base/default.j2', file: UploadFile = File(...) ): if await allowed_csv(file): await save_file(file) filename = file.filename with open(filename, 'r') as file: data = file.read() obj = json.loads(data) for key in obj: name = key['firstname'] + ' ' + key['lastname'] email = key['email'] html = await render_template(htmlfile, context, name) await send_email( receiver=email, sender=sender, password=password, subject=subject, body=html, host=host, port=port ) time.sleep(5) os.remove(filename) return {'message': 'sent message success !'}
async def compare_faces(face1: UploadFile = File(...), face2: UploadFile = File(...)): # for concurrency # `create_task` is only for Python 3.7+ # `ensure_future` is its alternative get_face1_bytes_task = asyncio.ensure_future(face1.read()) get_face2_bytes_task = asyncio.ensure_future(face2.read()) face1_bytes = await get_face1_bytes_task face2_bytes = await get_face2_bytes_task # convert to cv2 array img_np_arr = np.frombuffer(face1_bytes, np.uint8) f1 = cv2.imdecode(img_np_arr, cv2.IMREAD_COLOR) img_np_arr = np.frombuffer(face2_bytes, np.uint8) f2 = cv2.imdecode(img_np_arr, cv2.IMREAD_COLOR) # compare sim, dist = compare_two_faces(f1, f2) # convert to NORMAL float from `numpy.float32` return {'sim': float(sim), 'dist': float(dist)}
async def create_upload_file(file: UploadFile = File(...)): # when the user uploads a file, the filename will be stored in memory. # this is the file that global filename filename = file.filename code = file.read() # write code to code_dir async with aiofiles.open(f'{code_dir}/{filename}', 'wb') as upload: while content := await file.read(1024): await upload.write(content)
def upload_image(file: UploadFile): file_name = str(uuid.uuid4()) + file.filename.split(".")[-1] file = file.file accesskeyid = 'LTAIv816izYZhMkl' accesskey = 'Psfru0eOBJVFUELx9AsmLVeNsPnCh1' endpoint = 'oss-ap-southeast-1.aliyuncs.com' bucket_name = 'cashloan-ly' auth = oss2.Auth(accesskeyid, accesskey) bucket = oss2.Bucket(auth, endpoint=endpoint, bucket_name=bucket_name) bucket.put_object(file_name, file.read()) image_url = '%s://%s.%s/%s' % ('http', bucket_name, endpoint, file_name) return {'image': image_url}