Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add control flags for disabling upload/download/listing. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions updog/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from updog import version as VERSION


def read_write_directory(directory):
def check_read_write_directory(directory):
if os.path.exists(directory):
if os.access(directory, os.W_OK and os.R_OK):
return directory
Expand All @@ -22,11 +22,10 @@ def read_write_directory(directory):
else:
error('The specified directory does not exist')


def parse_arguments():
parser = argparse.ArgumentParser(prog='updog')
cwd = os.getcwd()
parser.add_argument('-d', '--directory', metavar='DIRECTORY', type=read_write_directory, default=cwd,
parser.add_argument('-d', '--directory', metavar='DIRECTORY', type=check_read_write_directory, default=cwd,
help='Root directory\n'
'[Default=.]')
parser.add_argument('-p', '--port', type=int, default=9090,
Expand All @@ -35,6 +34,11 @@ def parse_arguments():
parser.add_argument('--ssl', action='store_true', help='Use an encrypted connection')
parser.add_argument('--version', action='version', version='%(prog)s v'+VERSION)

# Content control flags
parser.add_argument('--no-download', action='store_true', help='Disable file download.')
parser.add_argument('--no-listing', action='store_true', help='Disable file listing.')
parser.add_argument('--no-upload', action='store_true', help='Disable file upload.')

args = parser.parse_args()

# Normalize the path
Expand Down Expand Up @@ -79,6 +83,9 @@ def home(path):
# If file
elif os.path.isfile(requested_path):

if args.no_download:
abort(403, 'Read Permission Denied: ' + requested_path)

# Check if the view flag is set
if request.args.get('view') is None:
send_as_attachment = True
Expand All @@ -103,15 +110,32 @@ def home(path):
requested_path = base_directory
back = ''

if args.no_listing and requested_path != base_directory:
return redirect('/')

if os.path.exists(requested_path):
# Read the files
try:
directory_files = process_files(os.scandir(requested_path), base_directory)
except PermissionError:
abort(403, 'Read Permission Denied: ' + requested_path)

return render_template('home.html', files=directory_files, back=back,
directory=requested_path, is_subdirectory=is_subdirectory, version=VERSION)
if args.no_listing:
directory_files = []
else:
try:
directory_files = process_files(os.scandir(requested_path), base_directory)
except PermissionError:
abort(403, 'Read Permission Denied: ' + requested_path)


return render_template(
'home.html',
files=directory_files,
allow_download = not args.no_download,
allow_listing = not args.no_listing,
allow_upload = not args.no_upload,
back=back,
directory=requested_path,
is_subdirectory=is_subdirectory,
version=VERSION
)
else:
return redirect('/')

Expand All @@ -123,6 +147,9 @@ def home(path):
def upload():
if request.method == 'POST':

if args.no_upload:
abort(405, 'Uploads are not allowed.')

# No file part - needs to check before accessing the files['file']
if 'file' not in request.files:
return redirect(request.referrer)
Expand Down
10 changes: 9 additions & 1 deletion updog/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h2>Directory: {{ directory }}</h2>
</div>
</header>

{% if allow_upload %}
<!-- ----- Upload Form ----- -->
<div class="inputUploadP">
<form method="post" action="/upload" enctype="multipart/form-data" class="uploadForm">
Expand All @@ -49,6 +50,7 @@ <h2>Directory: {{ directory }}</h2>
</p>
</form>
</div>
{% endif %}

{% if is_subdirectory %}
<section class="backBtn_p">
Expand All @@ -59,6 +61,7 @@ <h2>Directory: {{ directory }}</h2>
</section>
{% endif %}

{% if allow_listing %}
<!-- Table -->
<section class="table_p table-responsive">
<table id="tableData" class="table table-hover compact">
Expand All @@ -82,7 +85,11 @@ <h2>Directory: {{ directory }}</h2>
{% endif %}
</td>
<td> <!-- Name -->
{% if file.is_dir or allow_download %}
<a href="/{{ file.rel_path }}">{{ file.name }}{% if file.is_dir %}/{% endif %}</a>
{% else %}
{{ file.name }}
{% endif %}
</td>
<td data-order="{{ file.size_sort }}"> <!-- File size -->
{{ file.size }}
Expand All @@ -100,6 +107,7 @@ <h2>Directory: {{ directory }}</h2>
</tbody>
</table>
</section>
{% endif %}

<footer>
<p>
Expand All @@ -115,4 +123,4 @@ <h2>Directory: {{ directory }}</h2>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>

</body>
</html>
</html>