MCPcopy
hub / github.com/WebODM/WebODM / post

Method post

app/api/tasks.py:842–927  ·  view source on GitHub ↗
(self, request, project_pk=None)

Source from the content-addressed store, hash-verified

840 parser_classes = (parsers.MultiPartParser, parsers.JSONParser, parsers.FormParser,)
841
842 def post(self, request, project_pk=None):
843 project = get_and_check_project(request, project_pk, ('change_project',))
844 files = flatten_files(request.FILES)
845 tmp_upload_dir = get_external_import_tmpdir(request)
846
847 if len(files) != 1:
848 raise exceptions.ValidationError(detail=_("Cannot create task, you need to upload 1 file"))
849
850 file_type = [k for k in request.FILES][0]
851 file_ext = os.path.splitext(files[0].name)[1]
852
853 asset_file = None
854 asset_file_candidates = EXTERNAL_ASSET_FILES.get(file_type)
855 if asset_file_candidates is None:
856 raise exceptions.ValidationError(detail=_("Invalid file type"))
857
858 ext_aliases = {
859 '.tiff': '.tif'
860 }
861 for f in asset_file_candidates:
862 if os.path.splitext(f)[1].lower() == ext_aliases.get(file_ext.lower(), file_ext.lower()):
863 asset_file = f
864 break
865
866 if asset_file is None:
867 raise exceptions.ValidationError(detail=_("Invalid file type (extension mismatch)"))
868
869 chunk_index = request.data.get('dzchunkindex')
870 uuid = request.data.get('dzuuid')
871 total_chunk_count = request.data.get('dztotalchunkcount', None)
872
873 # 50% of the time, raise an exception
874 # import random
875 # if random.random() < 0.5:
876 # import time
877 # time.sleep(2)
878 # return Response('', status=524)
879 # raise exceptions.ValidationError(detail=_("Random upload failure for testing"))
880
881 # Chunked upload?
882 tmp_upload_file = None
883 if len(files) > 0 and chunk_index is not None and uuid is not None and total_chunk_count is not None:
884 byte_offset = request.data.get('dzchunkbyteoffset', 0)
885
886 try:
887 chunk_index = int(chunk_index)
888 byte_offset = int(byte_offset)
889 total_chunk_count = int(total_chunk_count)
890 except ValueError:
891 raise exceptions.ValidationError(detail=_("Some parameters are not integers"))
892 uuid = re.sub('[^0-9a-zA-Z-]+', "", uuid)
893
894 tmp_upload_file = os.path.join(tmp_upload_dir, f"{uuid}.upload")
895 if os.path.isfile(tmp_upload_file) and chunk_index == 0:
896 os.unlink(tmp_upload_file)
897
898 with open(tmp_upload_file, 'ab') as fd:
899 fd.truncate(byte_offset)

Callers

nothing calls this directly

Calls 7

get_and_check_projectFunction · 0.85
flatten_filesFunction · 0.85
writeMethod · 0.80
temporary_file_pathMethod · 0.80
_Function · 0.50
getMethod · 0.45

Tested by

no test coverage detected