示例#1
0
def upload_file(request):
	""" Function: upload_file
		Pre-Processing: Auth check
		Param(s): request	an HttpRequestObject
		This function handles the upload of file(s). File info
			is fetched by processing the file with an id3 tag reader. The
			actual file is then encrypted and saved to disk.
	"""
	if request.method == 'POST':
		f = request.FILES['Filedata']
		logging.debug("got file with name: " + f.name)
		mf = MediaFile()
		mf.type = f.content_type
		mf.name = f.name
		mf.size = f.size
		
		# attempt to get info from id3 tag
		id3r = id3reader.Reader(f.temporary_file_path())
		mf.populate_from_id3reader(id3r)
		
		# calculate the location
		sh = hashlib.sha1(mf.title)
		mf.s3_bucket = 'files'
		mf.s3_key = sh	

		mf.save()
		
		# create the 128-bit key for file encryption
		secret = create_secret()
		mfk = MediaFileKey(media_id=mf.id, key=secret)
		mfk.save()
		
		# encrypt the file w AES
		enc = encryption.FileCrypt(f, mfk.key)
		encrypted_bytes = enc.encrypt()

		s3_controller = S3Controller() 
		
		s3_controller.create_bucket(mf.s3_bucket)
		s3_controller.put_file(mf.s3_bucket, encrypted_bytes, mf.s3_key)
		
		return HttpResponse(True)
示例#2
0
文件: views.py 项目: mlewis/GFriends
def upload_file(request):
	""" Function: upload_file
		Pre-Processing: Auth check
		Param(s): request	an HttpRequestObject
		This function handles the upload of file(s). File info
			is fetched by processing the file with an id3 tag reader. The
			actual file is then encrypted and saved to disk.
	"""
	if request.method == 'POST':
		f = request.FILES['Filedata']
		logging.debug("got file with name: " + f.name)
		mf = MediaFile()
		mf.type = f.content_type
		mf.name = f.name
		mf.size = f.size
		
		# attempt to get info from id3 tag
		id3r = id3reader.Reader(f.temporary_file_path())
		mf.populate_from_id3reader(id3r)
		
		# calculate the location
		sh = hashlib.sha1(mf.title)
		mf.location = sh.hexdigest()[:10]
		
		mf.save()
		
		# create the 128-bit key for file encryption
		secret = create_secret()
		mfk = MediaFileKey(media_id=mf.id, key=secret)
		mfk.save()
		
		# encrypt the file w AES
		enc = encryption.FileCrypt(f, mfk.key)
		encrypted_bytes = enc.encrypt()
		
		# write to disk
		file_name = settings.MEDIA_ROOT + mf.location
		fs = open(file_name, 'w')
		fs.write(encrypted_bytes)
		fs.close()

		return HttpResponse(True)