Better to use os.path.join for paths

This commit is contained in:
Antoine Bartuccio 2017-03-30 19:13:47 +02:00
parent 1f844da005
commit 60e2e0d4f9
4 changed files with 16 additions and 11 deletions

View File

@ -12,6 +12,8 @@ from django.contrib.staticfiles.storage import staticfiles_storage
from django.utils.html import escape from django.utils.html import escape
from django.utils.functional import cached_property from django.utils.functional import cached_property
import os
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
@ -664,10 +666,10 @@ class SithFile(models.Model):
if self.is_folder: if self.is_folder:
for c in self.children.all(): for c in self.children.all():
c.move_to(self) c.move_to(self)
shutil.rmtree(settings.MEDIA_ROOT + old_file_name) shutil.rmtree(os.path.join(settings.MEDIA_ROOT, old_file_name))
else: else:
self.file.save(name=self.name, content=self.file) self.file.save(name=self.name, content=self.file)
os.remove(settings.MEDIA_ROOT + old_file_name) os.remove(os.path.join(settings.MEDIA_ROOT, old_file_name))
def __getattribute__(self, attr): def __getattribute__(self, attr):
if attr == "is_file": if attr == "is_file":

View File

@ -38,10 +38,11 @@ def send_file(request, file_id, file_class=SithFile, file_attr="file"):
): ):
raise PermissionDenied raise PermissionDenied
name = f.__getattribute__(file_attr).name name = f.__getattribute__(file_attr).name
with open((settings.MEDIA_ROOT + name).encode('utf-8'), 'rb') as filename: filepath = os.path.join(settings.MEDIA_ROOT, name)
with open(filepath.encode('utf-8'), 'rb') as filename:
wrapper = FileWrapper(filename) wrapper = FileWrapper(filename)
response = HttpResponse(wrapper, content_type=f.mime_type) response = HttpResponse(wrapper, content_type=f.mime_type)
response['Content-Length'] = os.path.getsize((settings.MEDIA_ROOT + name).encode('utf-8')) response['Content-Length'] = os.path.getsize(filepath.encode('utf-8'))
response['Content-Disposition'] = ('inline; filename="%s"' % f.name).encode('utf-8') response['Content-Disposition'] = ('inline; filename="%s"' % f.name).encode('utf-8')
return response return response

View File

@ -7,6 +7,7 @@ from django.core.files.base import ContentFile
from PIL import Image from PIL import Image
from io import BytesIO from io import BytesIO
import os
from core.models import SithFile, User from core.models import SithFile, User
from core.utils import resize_image, exif_auto_rotate from core.utils import resize_image, exif_auto_rotate
@ -17,7 +18,7 @@ class Picture(SithFile):
@property @property
def is_vertical(self): def is_vertical(self):
with open((settings.MEDIA_ROOT + self.file.name).encode('utf-8'), 'rb') as f: with open(os.path.join(settings.MEDIA_ROOT, self.file.name).encode('utf-8'), 'rb') as f:
im = Image.open(BytesIO(f.read())) im = Image.open(BytesIO(f.read()))
(w, h) = im.size (w, h) = im.size
return (w / h) < 1 return (w / h) < 1
@ -67,7 +68,7 @@ class Picture(SithFile):
def rotate(self, degree): def rotate(self, degree):
for attr in ['file', 'compressed', 'thumbnail']: for attr in ['file', 'compressed', 'thumbnail']:
name = self.__getattribute__(attr).name name = self.__getattribute__(attr).name
with open((settings.MEDIA_ROOT + name).encode('utf-8'), 'r+b') as file: with open(os.path.join(settings.MEDIA_ROOT, name).encode('utf-8'), 'r+b') as file:
if file: if file:
im = Image.open(BytesIO(file.read())) im = Image.open(BytesIO(file.read()))
file.seek(0) file.seek(0)

View File

@ -21,11 +21,12 @@
} }
</style> </style>
<link rel="prefetch" href="{{ url("sas:picture", picture_id=picture.get_previous().id} }}"> <!-- Firefox --> {% if picture.get_previous() %}
<link rel="prerender" href="{{ url("sas:picture", picture_id=picture.get_previous().id }}"> <!-- Chrome --> <link rel="preload" as="image" href="{{ url("sas:download_compressed", picture_id=picture.get_previous().id) }}">
{% endif %}
<link rel="prefetch" href="{{ url("sas:picture", picture_id=picture.get_next().id} }}"> <!-- Firefox --> {% if picture.get_next() %}
<link rel="prerender" href="{{ url("sas:picture", picture_id=picture.get_next().id }}"> <!-- Chrome --> <link rel="preload" as="image" href="{{ url("sas:download_compressed", picture_id=picture.get_next().id) }}">
{% endif %}
{% endblock %} {% endblock %}