|
8 | 8 | import httpx |
9 | 9 | import traitlets |
10 | 10 | import base64 |
| 11 | +from io import BytesIO |
11 | 12 | from jupyter_server.utils import url_path_join |
12 | 13 |
|
13 | 14 | import obstore as obs |
@@ -183,6 +184,9 @@ async def get_contents(self, drive_name, path): |
183 | 184 | """ |
184 | 185 | if path == '/': |
185 | 186 | path = '' |
| 187 | + else: |
| 188 | + path = path.strip('/') |
| 189 | + |
186 | 190 | try : |
187 | 191 | data = [] |
188 | 192 | isDir = False |
@@ -255,23 +259,198 @@ async def get_contents(self, drive_name, path): |
255 | 259 |
|
256 | 260 | return response |
257 | 261 |
|
258 | | - async def new_file(self, drive_name, path, **kwargs): |
| 262 | + async def new_file(self, drive_name, path): |
259 | 263 | """Create a new file or directory at the given path. |
260 | 264 | |
261 | 265 | Args: |
262 | 266 | drive_name: name of drive where the new content is created |
263 | 267 | path: path where new content should be created |
264 | 268 | """ |
265 | | - print('New file function called.') |
| 269 | + data = {} |
| 270 | + try: |
| 271 | + # eliminate leading and trailing backslashes |
| 272 | + path = path.strip('/') |
| 273 | + |
| 274 | + # TO DO: switch to mode "created", which is not implemented yet |
| 275 | + await obs.put_async(self._content_managers[drive_name], path, b"", mode = "overwrite") |
| 276 | + metadata = await obs.head_async(self._content_managers[drive_name], path) |
| 277 | + |
| 278 | + data = { |
| 279 | + "path": path, |
| 280 | + "content": "", |
| 281 | + "last_modified": metadata["last_modified"].isoformat(), |
| 282 | + "size": metadata["size"] |
| 283 | + } |
| 284 | + except Exception as e: |
| 285 | + raise tornado.web.HTTPError( |
| 286 | + status_code= httpx.codes.BAD_REQUEST, |
| 287 | + reason=f"The following error occured when creating the object: {e}", |
| 288 | + ) |
| 289 | + |
| 290 | + response = { |
| 291 | + "data": data |
| 292 | + } |
| 293 | + return response |
| 294 | + |
| 295 | + async def save_file(self, drive_name, path, content, options_format, content_format, content_type): |
| 296 | + """Save file with new content. |
| 297 | + |
| 298 | + Args: |
| 299 | + drive_name: name of drive where file exists |
| 300 | + path: path where new content should be saved |
| 301 | + content: content of object |
| 302 | + options_format: format of content (as sent through contents manager request) |
| 303 | + content_format: format of content (as defined by the registered file formats in JupyterLab) |
| 304 | + content_type: type of content (as defined by the registered file types in JupyterLab) |
| 305 | + """ |
| 306 | + data = {} |
| 307 | + try: |
| 308 | + # eliminate leading and trailing backslashes |
| 309 | + path = path.strip('/') |
| 310 | + |
| 311 | + if options_format == 'json': |
| 312 | + formatted_content = json.dumps(content, indent=2) |
| 313 | + formatted_content = formatted_content.encode("utf-8") |
| 314 | + elif options_format == 'base64' and (content_format == 'base64' or content_type == 'PDF'): |
| 315 | + # transform base64 encoding to a UTF-8 byte array for saving or storing |
| 316 | + byte_characters = base64.b64decode(content) |
| 317 | + |
| 318 | + byte_arrays = [] |
| 319 | + for offset in range(0, len(byte_characters), 512): |
| 320 | + slice_ = byte_characters[offset:offset + 512] |
| 321 | + byte_array = bytearray(slice_) |
| 322 | + byte_arrays.append(byte_array) |
| 323 | + |
| 324 | + # combine byte arrays and wrap in a BytesIO object |
| 325 | + formatted_content = BytesIO(b"".join(byte_arrays)) |
| 326 | + formatted_content.seek(0) # reset cursor for any further reading |
| 327 | + elif options_format == 'text': |
| 328 | + formatted_content = content.encode("utf-8") |
| 329 | + else: |
| 330 | + formatted_content = content |
| 331 | + |
| 332 | + await obs.put_async(self._content_managers[drive_name], path, formatted_content, mode = "overwrite") |
| 333 | + metadata = await obs.head_async(self._content_managers[drive_name], path) |
| 334 | + |
| 335 | + data = { |
| 336 | + "path": path, |
| 337 | + "content": content, |
| 338 | + "last_modified": metadata["last_modified"].isoformat(), |
| 339 | + "size": metadata["size"] |
| 340 | + } |
| 341 | + except Exception as e: |
| 342 | + raise tornado.web.HTTPError( |
| 343 | + status_code= httpx.codes.BAD_REQUEST, |
| 344 | + reason=f"The following error occured when saving the file: {e}", |
| 345 | + ) |
| 346 | + |
| 347 | + response = { |
| 348 | + "data": data |
| 349 | + } |
| 350 | + return response |
266 | 351 |
|
267 | | - async def rename_file(self, drive_name, path, **kwargs): |
| 352 | + async def rename_file(self, drive_name, path, new_path): |
268 | 353 | """Rename a file. |
269 | 354 | |
270 | 355 | Args: |
271 | 356 | drive_name: name of drive where file is located |
272 | 357 | path: path of file |
| 358 | + new_path: path of new file name |
273 | 359 | """ |
274 | | - print('Rename file function called.') |
| 360 | + data = {} |
| 361 | + try: |
| 362 | + # eliminate leading and trailing backslashes |
| 363 | + path = path.strip('/') |
| 364 | + |
| 365 | + await obs.rename_async(self._content_managers[drive_name], path, new_path) |
| 366 | + metadata = await obs.head_async(self._content_managers[drive_name], new_path) |
| 367 | + |
| 368 | + data = { |
| 369 | + "path": new_path, |
| 370 | + "last_modified": metadata["last_modified"].isoformat(), |
| 371 | + "size": metadata["size"] |
| 372 | + } |
| 373 | + except Exception as e: |
| 374 | + raise tornado.web.HTTPError( |
| 375 | + status_code= httpx.codes.BAD_REQUEST, |
| 376 | + reason=f"The following error occured when renaming the object: {e}", |
| 377 | + ) |
| 378 | + |
| 379 | + response = { |
| 380 | + "data": data |
| 381 | + } |
| 382 | + return response |
| 383 | + |
| 384 | + async def delete_file(self, drive_name, path): |
| 385 | + """Delete an object. |
| 386 | + |
| 387 | + Args: |
| 388 | + drive_name: name of drive where object exists |
| 389 | + path: path where content is located |
| 390 | + """ |
| 391 | + try: |
| 392 | + # eliminate leading and trailing backslashes |
| 393 | + path = path.strip('/') |
| 394 | + await obs.delete_async(self._content_managers[drive_name], path) |
| 395 | + |
| 396 | + except Exception as e: |
| 397 | + raise tornado.web.HTTPError( |
| 398 | + status_code= httpx.codes.BAD_REQUEST, |
| 399 | + reason=f"The following error occured when deleting the object: {e}", |
| 400 | + ) |
| 401 | + |
| 402 | + return |
| 403 | + |
| 404 | + async def check_file(self, drive_name, path): |
| 405 | + """Check if an object already exists within a drive. |
| 406 | + |
| 407 | + Args: |
| 408 | + drive_name: name of drive where object exists |
| 409 | + path: path where content is located |
| 410 | + """ |
| 411 | + try: |
| 412 | + # eliminate leading and trailing backslashes |
| 413 | + path = path.strip('/') |
| 414 | + await obs.head_async(self._content_managers[drive_name], path) |
| 415 | + except Exception: |
| 416 | + raise tornado.web.HTTPError( |
| 417 | + status_code= httpx.codes.NOT_FOUND, |
| 418 | + reason="Object does not already exist within drive.", |
| 419 | + ) |
| 420 | + |
| 421 | + return |
| 422 | + |
| 423 | + async def copy_file(self, drive_name, path, to_path): |
| 424 | + """Save file with new content. |
| 425 | + |
| 426 | + Args: |
| 427 | + drive_name: name of drive where file exists |
| 428 | + path: path where original content exists |
| 429 | + to_path: path where object should be copied |
| 430 | + """ |
| 431 | + data = {} |
| 432 | + try: |
| 433 | + # eliminate leading and trailing backslashes |
| 434 | + path = path.strip('/') |
| 435 | + |
| 436 | + await obs.copy_async(self._content_managers[drive_name], path, to_path) |
| 437 | + metadata = await obs.head_async(self._content_managers[drive_name], to_path) |
| 438 | + |
| 439 | + data = { |
| 440 | + "path": to_path, |
| 441 | + "last_modified": metadata["last_modified"].isoformat(), |
| 442 | + "size": metadata["size"] |
| 443 | + } |
| 444 | + except Exception as e: |
| 445 | + raise tornado.web.HTTPError( |
| 446 | + status_code= httpx.codes.BAD_REQUEST, |
| 447 | + reason=f"The following error occured when copying the: {e}", |
| 448 | + ) |
| 449 | + |
| 450 | + response = { |
| 451 | + "data": data |
| 452 | + } |
| 453 | + return response |
275 | 454 |
|
276 | 455 | def _get_drive_location(self, drive_name): |
277 | 456 | """Helping function for getting drive region. |
|
0 commit comments