|
2 | 2 | import json |
3 | 3 | import logging |
4 | 4 | from typing import Dict, List, Optional, Tuple, Union, Any |
5 | | -from datetime import timedelta |
| 5 | +from datetime import timedelta, datetime |
6 | 6 |
|
7 | 7 | import os |
8 | 8 | import tornado |
@@ -309,27 +309,31 @@ async def get_contents(self, drive_name, path): |
309 | 309 |
|
310 | 310 | return response |
311 | 311 |
|
312 | | - async def new_file(self, drive_name, path): |
| 312 | + async def new_file(self, drive_name, path, type): |
313 | 313 | """Create a new file or directory at the given path. |
314 | 314 | |
315 | 315 | Args: |
316 | 316 | drive_name: name of drive where the new content is created |
317 | 317 | path: path where new content should be created |
| 318 | + type: whether we are dealing with a file or a directory |
318 | 319 | """ |
319 | 320 | data = {} |
320 | 321 | try: |
321 | 322 | # eliminate leading and trailing backslashes |
322 | 323 | path = path.strip('/') |
323 | 324 |
|
324 | | - # TO DO: switch to mode "created", which is not implemented yet |
325 | | - await obs.put_async(self._content_managers[drive_name]["store"], path, b"", mode = "overwrite") |
326 | | - metadata = await obs.head_async(self._content_managers[drive_name]["store"], path) |
327 | | - |
| 325 | + if type == 'directory' and self._config.provider == 's3': |
| 326 | + await self._create_dir(drive_name, path) |
| 327 | + else: |
| 328 | + await self._file_system._touch(drive_name + '/' + path) |
| 329 | + metadata = await self._file_system._info(drive_name + '/' + path) |
| 330 | + |
328 | 331 | data = { |
329 | 332 | "path": path, |
330 | 333 | "content": "", |
331 | | - "last_modified": metadata["last_modified"].isoformat(), |
332 | | - "size": metadata["size"] |
| 334 | + "last_modified": metadata["LastModified"].isoformat() if metadata["type"]=='file' else datetime.now().isoformat(), |
| 335 | + "size": metadata["size"], |
| 336 | + "type": metadata["type"] |
333 | 337 | } |
334 | 338 | except Exception as e: |
335 | 339 | raise tornado.web.HTTPError( |
@@ -592,6 +596,24 @@ async def _check_object(self, drive_name, path): |
592 | 596 |
|
593 | 597 | return isDir |
594 | 598 |
|
| 599 | + async def _create_dir(self, drive_name, path): |
| 600 | + """Helping function to create an empty directory. |
| 601 | +
|
| 602 | + Args: |
| 603 | + drive_name: name of drive where object should be created |
| 604 | + path: path to object to create |
| 605 | + """ |
| 606 | + try: |
| 607 | + async with self._s3_session.create_client('s3', aws_secret_access_key=self._config.secret_access_key, aws_access_key_id=self._config.access_key_id, aws_session_token=self._config.session_token) as client: |
| 608 | + await client.put_object(Bucket=drive_name, Key=path + '/') |
| 609 | + except Exception as e: |
| 610 | + raise tornado.web.HTTPError( |
| 611 | + status_code= httpx.codes.BAD_REQUEST, |
| 612 | + reason=f"The following error occured when creating the directory: {e}", |
| 613 | + ) |
| 614 | + |
| 615 | + return |
| 616 | + |
595 | 617 | async def _call_provider( |
596 | 618 | self, |
597 | 619 | url: str, |
|
0 commit comments