Skip to content

Commit

Permalink
Use tempfile for generating the downloaded zip
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Posselt committed Jun 23, 2016
1 parent 795b87f commit bbe175b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
4 changes: 4 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ To get your instance running in production you need to create your local setting
'app_upload_or_delete': '20/day'
}
# Only set this parameter if you want to use a different tmp directory
# for temporary app downloads. Otherwise will default to /tmp
RELEASE_DOWNLOAD_ROOT = '/var/www/production-domain.com/tmp/'
.. note:: Absolutely make sure to generate a new **SECRET_KEY** value! Use the following command for instance to generate a token:

Expand Down
38 changes: 21 additions & 17 deletions nextcloudappstore/core/api/v1/release/downloader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import tempfile

import requests
import uuid
import os
Expand Down Expand Up @@ -43,40 +45,42 @@ def get_archive(self, url: str, target_directory: str, timeout: int = 60,
:raises requests.TooManyRedirects of too many redirects were made
:return the path to the downloaded file
"""
os.makedirs(target_directory, mode=0o755, exist_ok=True)

unique_file_name = uuid.uuid4().hex
filename = os.path.join(target_directory, unique_file_name)
if target_directory is None:
file = tempfile.NamedTemporaryFile(delete=False)
else:
os.makedirs(target_directory, mode=0o755, exist_ok=True)
file = tempfile.NamedTemporaryFile(dir=target_directory,
delete=False)

with requests.Session() as session:
session.max_redirects = max_redirects
req = session.get(url, stream=True, timeout=timeout)
req.raise_for_status()
if int(req.headers.get('Content-Length')) > max_size:
msg = 'Downloaded archive is bigger than the allowed %i ' \
'bytes' % max_size
'bytes' % max_size
raise MaximumDownloadSizeExceededException(msg)

self._stream_to_file(filename, max_size, req)
self._stream_to_file(file, max_size, req)

return ReleaseDownload(filename)
return ReleaseDownload(file.name)

def _stream_to_file(self, filename: str, max_size: int,
def _stream_to_file(self, file: Any, max_size: int,
req: requests.Response) -> None:
# start streaming download
finished = False
try:
with open(filename, 'wb') as fd:
size = 0
for chunk in req.iter_content(1024):
fd.write(chunk)
size += len(chunk)
if size > max_size:
msg = 'Downloaded archive is bigger than the ' \
'allowed %i bytes' % max_size
raise MaximumDownloadSizeExceededException(msg)
size = 0
for chunk in req.iter_content(1024):
file.write(chunk)
size += len(chunk)
if size > max_size:
msg = 'Downloaded archive is bigger than the ' \
'allowed %i bytes' % max_size
raise MaximumDownloadSizeExceededException(msg)
finished = True
finally:
# in case any errors occurred, get rid of the file
if not finished:
os.remove(filename)
os.remove(file.name)
14 changes: 7 additions & 7 deletions nextcloudappstore/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-21 18:51
# Generated by Django 1.9.7 on 2016-06-23 16:15
from __future__ import unicode_literals

from django.conf import settings
Expand Down Expand Up @@ -51,8 +51,8 @@ class Migration(migrations.Migration):
],
options={
'verbose_name': 'App Release',
'ordering': ['-version'],
'verbose_name_plural': 'App Releases',
'ordering': ['-version'],
},
),
migrations.CreateModel(
Expand All @@ -65,11 +65,11 @@ class Migration(migrations.Migration):
('master', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='core.App')),
],
options={
'managed': True,
'default_permissions': (),
'db_tablespace': '',
'verbose_name': 'App Translation',
'managed': True,
'db_table': 'core_app_translation',
'db_tablespace': '',
},
),
migrations.CreateModel(
Expand All @@ -95,11 +95,11 @@ class Migration(migrations.Migration):
('master', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='core.Category')),
],
options={
'managed': True,
'default_permissions': (),
'db_tablespace': '',
'verbose_name': 'Category Translation',
'managed': True,
'db_table': 'core_category_translation',
'db_tablespace': '',
},
),
migrations.CreateModel(
Expand Down Expand Up @@ -170,8 +170,8 @@ class Migration(migrations.Migration):
],
options={
'verbose_name': 'Screenshot',
'ordering': ['ordering'],
'verbose_name_plural': 'Screenshots',
'ordering': ['ordering'],
},
),
migrations.CreateModel(
Expand Down
2 changes: 1 addition & 1 deletion nextcloudappstore/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
os.path.join(BASE_DIR, 'static'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
RELEASE_DOWNLOAD_ROOT = os.path.join(MEDIA_ROOT, 'releasetmp')
RELEASE_DOWNLOAD_ROOT = None
STATIC_URL = '/static/'
MEDIA_URL = '/media/'

Expand Down

0 comments on commit bbe175b

Please sign in to comment.