Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-24ddc0f5d75046c5622901739e7c5dd533143b0c8e959d652212380cedb1ea36.svg)](https://classroom.github.com/a/wjmO5Bst)
[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-718a45dd9cf7e7f842a935f5ebbe5719a5e09af4491e668f4dbf3b35d5cca122.svg)](https://classroom.github.com/online_ide?assignment_repo_id=10989966&assignment_repo_type=AssignmentRepo)
95 changes: 95 additions & 0 deletions project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import zipfile
import tempfile
from flask import(
Flask,
request,
render_template,
redirect,
session,
url_for,

)
from werkzeug.utils import secure_filename
import spwd
import crypt

app = Flask(__name__)
app.secret_key = os.urandom(24)

@app.route('/')
def index():
if 'username' in session:
return redirect(url_for('browse'))
return render_template(('login.html'))

@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

if authenticate(username, password):
session['username'] = username
return redirect(url_for('browse'))
else:
error = 'Invalid credentials'
return render_template('login.html', error=error)

def authenticate(username, password):
try:
user = spwd.getspnam(username)
if user:
return crypt.crypt(password, user.sp_pwd) == user.sp_pwd
except KeyError:
return False
return False

@app.route('/logout')
def logout():
session.pop('username', None)
print('Logged out')
return redirect(url_for('index'))

@app.route('/browse', defaults={'path': ''})
@app.route('/browse/<path:path>')
def browse(path):
if 'username' not in session:
return redirect(url_for('login'))
base_dir = os.path.expanduser(f'~{session["username"]}')
full_path = os.path.join(base_dir, path)
return render_template('browse.html', path=path, content=list_directory(full_path))

def list_directory(path):
items = []
for item in os.listdir(path):
item_path = os.path.join(path, item)
items.append({
'name': item,
'is_file': os.path.isfile(item_path),
'size': os.path.getsize(item_path),
'path': item_path
})
return items

@app.route('/download')
def download():
if 'username' not in session:
return redirect(url_for('login'))

base_dir = os.path.expanduser(f'~{session["username"]}')
zip_filename = f'{session["username"]}_home.zip'

with tempfile.TemporaryDirectory() as temp_dir:
temp_zip_path = os.path.join(temp_dir, zip_filename)
with zipfile.ZipFile(temp_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(base_dir):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, base_dir)
zipf.write(file_path, arcname)
return send_file(temp_zip_path, as_attachment=True, attachment_filename=zip_filename)

if __name__ == '__main__':
app.run(host="0.0.0.0",debug=True)
55 changes: 55 additions & 0 deletions template/css/directory.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.buttons button {
margin-top: 60px;
width: 100px;
height: 50px;
margin-right: 30px;
border-radius: 20px;
color: white;
font-size: 20px;
border: none;
cursor: pointer;
}

#download {
width: 50px;
height: 50px;
cursor: pointer;
position: absolute;
left: 360px;
top: 165px;
}

#power-off {
width: 30px;
height: 30px;
cursor: pointer;
position: absolute;
right: 250px;
}

#search-bar-icon {
top: 165px;
width: 20px;
height: 20px;
cursor: pointer;
}

.search-bar {
margin-top: 10px;
margin-bottom: 20px;
}

.search-bar input {
padding-left: 50px;
border: 2px solid black;
border-radius: 20px;
}

table thead th {
font-family: Arial, Helvetica, sans-serif;
}

h3{
margin-left: 60px;
}

35 changes: 35 additions & 0 deletions template/directory.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>directory</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
/>
<link rel="stylesheet" href="/project/template/css/directory.css" />
</head>
<body>
<div class="container">
<div class="buttons">
<button style="background-color: #ff42a1">Files</button>
<button style="background-color: #00a1ff">Dirs</button>
<button style="background-color: #60d937">Space</button>
<img id="power-off" src="icones/on-off-button.png" alt="" />
</div>
<table>
<thead>
<th>Dossier &nbsp;&nbsp;&nbsp; |</th>

<th>
Date de
Modification &nbsp;&nbsp;&nbsp; |
</th>
<th>Taille</th>
</thead>
</table>
</div>
</body>
</html>
18 changes: 18 additions & 0 deletions template/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h1> test</h1>
<form method="post" action="/login">
<label for="username">username</label>
<input type="text" id="username" name="username">
<label for="password">password</label>
<input type="text" id="password" name="password">
</form>
</body>
</html>