Beispiel #1
0
def upload_file():
    # If a file is submitted to upload
    if flask.request.method == 'POST':
        # Check if the post request has the file part
        if 'file' not in flask.request.files:
            return 'Error : No file part'
        file = flask.request.files['file']
        # Check that file name is not empty
        if file.filename == '':
            return 'Error : No selected file'
        # Check that the file is allowed
        if not allowed_file(file.filename) :
            return 'Error : This file type is not allowed'
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            # Upload file
            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(filepath)
            # Unzip file
            z = zipfile.ZipFile(filepath)
            z.extractall(app.config['UPLOAD_FOLDER'])
            headers = dict()
            iamassaccess.createItems(filepath.replace('.zip', ''), headers)
            return 'Success : File uploaded and items created into Internet Archive'
    return '''
#
# Main
#

if __name__ == '__main__' :
	# Argument parser configuration + parsing of args
	parser = argparse.ArgumentParser(description='Bulk upload your items on archive.org, delete them or update their metadata!')
	parser.add_argument('mode', action='store', choices=['create', 'update', 'delete'], help="mode of operation : upload new items, update existing items' metadata or delete files")
	parser.add_argument('--metadata', dest='metadata', type=lambda x: is_valid_file(x), help="the metada csv file : headers + 1 row/file")
	parser.add_argument('--folder', dest='folder', type=lambda x: is_valid_folder(x), help="folder containing the files to be uploaded")
	args = parser.parse_args()

	# Check arguments and validity of mode + files provided
	if args.mode == 'create' and args.folder is None:
		logging.error('Mode chosen is CREATE and no files are provided to upload')
		sys.exit(0)
	elif args.mode == 'update' and args.metadata is None:
		logging.error('Mode chosen is UPDATE and no metadata file is provided to update files')
		sys.exit(0)
	elif args.mode == 'delete' and args.metadata is None:
		logging.error('Mode chosen is DELETE and no metadata file is provided to delete files')
		sys.exit(0)

	if args.mode == 'create':
		headers = dict()
		iamassaccess.createItems(args.folder, headers)
	elif args.mode == 'update':
		iamassaccess.updateItems(args.metadata)
	elif args.mode == 'delete':
		iamassaccess.deleteItems(args.metadata)