当前位置: 首页>>代码示例>>Python>>正文


Python File.sanitize_filename方法代码示例

本文整理汇总了Python中models.File.sanitize_filename方法的典型用法代码示例。如果您正苦于以下问题:Python File.sanitize_filename方法的具体用法?Python File.sanitize_filename怎么用?Python File.sanitize_filename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.File的用法示例。


在下文中一共展示了File.sanitize_filename方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: store

# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import sanitize_filename [as 别名]
def store(request):
	"""
	This view recieves a chunck of a file and saves it. When all the chunks are
	uploaded, they are joined together to make a complete file.

	This is gonna be pretty sketch for a while
	"""
	guid = File.sanitize_filename(request.POST['resumableIdentifier'])
	if not guid:
		return HttpResponseNotFound("Invalid file identifier")

	dir_path = os.path.join(settings.TMP_ROOT, str(request.user.pk) + "-" + guid)
	try:
		os.makedirs(dir_path)
	except OSError as e:
		# directory exists, dude!
		pass

	# each file will be named 1.part, 2.part etc. and stored inside the dir_path
	file_path = os.path.join(dir_path, str(int(request.POST['resumableChunkNumber'])) + '.part')
	file = request.FILES['file']

	# dont let that chunk be too big
	if file.size > (settings.CHUNK_SIZE*2):
		shutil.rmtree(dir_path)
		return HttpResponseNotFound("Too many chunks")

	with open(file_path, 'wb') as dest:
		for chunk in file.chunks():
			dest.write(chunk)

	total_number_of_chunks = int(request.POST['resumableTotalChunks'])
	total_number_of_uploaded_chunks = len(os.listdir(dir_path))
	if total_number_of_chunks != total_number_of_uploaded_chunks:
		return HttpResponse("OK")

	total_size = 0
	for i in range(1, total_number_of_chunks + 1):
		chunk_path = os.path.join(dir_path, str(i) + '.part')
		total_size += os.path.getsize(chunk_path)
		if total_size > settings.MAX_UPLOAD_SIZE:
			shutil.rmtree(dir_path)
			return HttpResponseNotFound("File too big")

	if total_size != int(request.POST['resumableTotalSize']):
		# All files present and accounted for, 
		# just slow as balls when it comes to getting written
		return HttpResponse("OK")

	try:
		f = File(
			name=request.POST['resumableFilename'],
			type=FileType.UNKNOWN,
			status=FileStatus.UPLOADED,
			uploaded_by=request.user,
			tmp_path=dir_path,
		)
		f.save()
	except DatabaseError as e:
		# that file was already taken care of, dude
		return HttpResponse("OK")

	process_uploaded_file.delay(total_number_of_chunks, f)
	return HttpResponse("COMPLETE")
开发者ID:kfarr2,项目名称:role_initiative,代码行数:66,代码来源:views.py


注:本文中的models.File.sanitize_filename方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。