patharanor commited on
Commit
0725aa3
·
1 Parent(s): 5b26209

fix: request schema

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -105,35 +105,42 @@ async def upload2(
105
  job_no: Annotated[str, Form()] = '',
106
  s3key: Annotated[str, Form()] = '',
107
  ref: Annotated[str, Form()] = '',
108
- file: Annotated[bytes, File()] = File(...)
109
  ):
110
  res = Response()
 
 
111
  logging.info("--------------------------------")
112
  logging.info("Received request to upload image")
113
-
114
  # Validate headers
115
  if not is_valid(x_request_user, x_api_key):
116
  res.status = HTTPStatus.FORBIDDEN
117
  res.error = "Invalid credentials"
118
- else:
119
- key = f'{s3key}/{job_no}/{file.filename}'
120
- logging.info(f'Key for S3 upload: {key}')
 
 
 
121
 
122
- try:
123
- # Read the file content directly from UploadFile
124
- file_content = await file.read()
125
- logging.info(f'File content length: {len(file_content)} bytes')
126
 
127
- # Upload file object to S3
128
- # logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
129
- res = s3client.upload_file(AWS_S3_BUCKET_NAME, file, key)
130
- logging.info("File uploaded successfully")
131
 
132
- except Exception as e:
133
- res.error = str(e)
134
- logging.warning(f"Error during upload: {res.error}")
 
 
 
 
 
135
 
136
- res.data = ref
137
  return res
138
 
139
  def is_valid(u, p):
 
105
  job_no: Annotated[str, Form()] = '',
106
  s3key: Annotated[str, Form()] = '',
107
  ref: Annotated[str, Form()] = '',
108
+ file: UploadFile = File(...)
109
  ):
110
  res = Response()
111
+ res.data = ref
112
+
113
  logging.info("--------------------------------")
114
  logging.info("Received request to upload image")
115
+
116
  # Validate headers
117
  if not is_valid(x_request_user, x_api_key):
118
  res.status = HTTPStatus.FORBIDDEN
119
  res.error = "Invalid credentials"
120
+ return res
121
+
122
+ if file is None:
123
+ res.status = HTTPStatus.BAD_REQUEST
124
+ res.error = "File not found"
125
+ return res
126
 
127
+ key = f'{s3key}/{job_no}/{file.filename}'
128
+ logging.info(f'Key for S3 upload: {key}')
 
 
129
 
130
+ try:
131
+ # Read the file content directly from UploadFile
132
+ file_content = await file.read()
133
+ logging.info(f'File content length: {len(file_content)} bytes')
134
 
135
+ # Upload file object to S3
136
+ # logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
137
+ res = s3client.upload_file(AWS_S3_BUCKET_NAME, file, key)
138
+ logging.info("File uploaded successfully")
139
+
140
+ except Exception as e:
141
+ res.error = str(e)
142
+ logging.warning(f"Error during upload: {res.error}")
143
 
 
144
  return res
145
 
146
  def is_valid(u, p):