Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Chiefs
ChiefSend
Commits
825898cc
Commit
825898cc
authored
Oct 02, 2020
by
Lukas Böhm
Browse files
cleanup timer works now
parent
1b16e96d
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/routes.py
View file @
825898cc
...
...
@@ -7,6 +7,50 @@ from flask import redirect, url_for, render_template, send_from_directory, curre
from
app.models
import
Share
,
Attachment
from
app.forms
import
UploadForm
from
shutil
import
rmtree
,
make_archive
from
apscheduler.schedulers.background
import
BackgroundScheduler
import
atexit
def
remove_expired
():
print
(
'CLEAING UP'
)
with
app
.
app_context
():
expired_shares
=
Share
.
query
.
filter
(
datetime
.
now
()
>
Share
.
expires
).
all
()
for
sh
in
expired_shares
:
# delete files
try
:
rmtree
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
sh
.
id
))
os
.
remove
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
sh
.
id
+
'.zip'
))
except
OSError
as
e
:
print
(
"Error: %s - %s."
%
(
e
.
filename
,
e
.
strerror
))
# delete from database
for
att
in
sh
.
files
:
db
.
session
.
delete
(
att
)
db
.
session
.
delete
(
sh
)
db
.
session
.
commit
()
sched
=
BackgroundScheduler
(
daemon
=
True
)
sched
.
add_job
(
remove_expired
,
'interval'
,
seconds
=
10
)
sched
.
start
()
atexit
.
register
(
lambda
:
sched
.
shutdown
())
def
check_limit
(
share
):
if
share
.
download_limit
<
0
or
share
.
expires
<
datetime
.
now
():
# delete files
try
:
rmtree
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
share
.
id
))
os
.
remove
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
share
.
id
+
'.zip'
))
except
OSError
as
e
:
print
(
"Error: %s - %s."
%
(
e
.
filename
,
e
.
strerror
))
# delete from database
for
att
in
share
.
files
:
db
.
session
.
delete
(
att
)
db
.
session
.
delete
(
share
)
db
.
session
.
commit
()
return
False
else
:
return
True
@
app
.
route
(
'/privacy'
)
...
...
@@ -40,7 +84,7 @@ def upload():
while
Share
.
query
.
get
(
share_id
)
is
not
None
:
share_id
=
str
(
uuid
.
uuid4
())
# create share object
up
=
Share
(
id
=
share_id
,
name
=
form
.
name
.
data
,
expires
=
datetime
.
now
()
+
timedelta
(
minutes
=
in
t
(
form
.
timer
.
data
)),
up
=
Share
(
id
=
share_id
,
name
=
form
.
name
.
data
,
expires
=
datetime
.
now
()
+
timedelta
(
minutes
=
floa
t
(
form
.
timer
.
data
)),
download_limit
=
form
.
download_limit
.
data
)
db
.
session
.
add
(
up
)
# create share folder
...
...
@@ -67,26 +111,10 @@ def upload():
return
render_template
(
'Upload.html'
,
form
=
form
)
def
check_expired
(
share
):
if
share
.
download_limit
<
0
or
datetime
.
now
()
>
share
.
expires
:
# delete files
try
:
rmtree
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
share
.
id
))
except
OSError
as
e
:
print
(
"Error: %s - %s."
%
(
e
.
filename
,
e
.
strerror
))
# delete from database
for
att
in
share
.
files
:
db
.
session
.
delete
(
att
)
db
.
session
.
delete
(
share
)
db
.
session
.
commit
()
return
True
return
False
@
app
.
route
(
'/shared/<string:share_id>'
)
def
shared
(
share_id
):
share
=
Share
.
query
.
get_or_404
(
share_id
)
check_
expired
(
share
)
check_
limit
(
share
)
return
render_template
(
'Shared.html'
,
url
=
url_for
(
'download'
,
share_id
=
share
.
id
))
...
...
@@ -95,16 +123,13 @@ def download(share_id):
share
=
Share
.
query
.
get_or_404
(
share_id
)
share
.
download_limit
-=
1
db
.
session
.
commit
()
if
check_expired
(
share
):
return
redirect
(
url_for
(
'expired'
))
else
:
return
render_template
(
'Download.html'
,
up
=
share
)
return
render_template
(
'Download.html'
,
up
=
share
)
@
app
.
route
(
'/media/<string:share_id>/<string:filename>'
,
methods
=
[
'GET'
])
def
media
(
share_id
,
filename
):
share
=
Share
.
query
.
get_or_404
(
share_id
)
check_
expired
(
share
)
check_
limit
(
share
)
return
send_from_directory
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
],
share_id
),
filename
=
filename
,
as_attachment
=
True
)
...
...
@@ -112,6 +137,6 @@ def media(share_id, filename):
@
app
.
route
(
'/zip/<string:share_id>'
)
def
zip
(
share_id
):
share
=
Share
.
query
.
get_or_404
(
share_id
)
check_
expired
(
share
)
check_
limit
(
share
)
return
send_from_directory
(
os
.
path
.
join
(
current_app
.
config
[
'MEDIA_LOCATION'
]),
filename
=
str
(
share
.
id
)
+
'.zip'
,
as_attachment
=
True
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment