mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-26 11:04:20 +00:00
core/models: better handle broken files
Signed-off-by: Skia <skia@libskia.so>
This commit is contained in:
parent
c7b918115d
commit
b2306b62d6
@ -782,7 +782,6 @@ class SithFile(models.Model):
|
|||||||
c._repair_fs()
|
c._repair_fs()
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
import os
|
|
||||||
# First get future parent path and the old file name
|
# First get future parent path and the old file name
|
||||||
# Prepend "." so that we match all relative handling of Django's
|
# Prepend "." so that we match all relative handling of Django's
|
||||||
# file storage
|
# file storage
|
||||||
@ -794,22 +793,32 @@ class SithFile(models.Model):
|
|||||||
new_path = "." + self.get_full_path()
|
new_path = "." + self.get_full_path()
|
||||||
print("Old path: %s " % old_path)
|
print("Old path: %s " % old_path)
|
||||||
print("New path: %s " % new_path)
|
print("New path: %s " % new_path)
|
||||||
# Make this atomic, so that a FS problem rolls back the DB change
|
try:
|
||||||
with transaction.atomic():
|
# Make this atomic, so that a FS problem rolls back the DB change
|
||||||
# Set the new filesystem path
|
with transaction.atomic():
|
||||||
self.file.name = new_path
|
# Set the new filesystem path
|
||||||
self.save()
|
self.file.name = new_path
|
||||||
print("New file path: %s " % self.file.path)
|
self.save()
|
||||||
# Really move at the FS level
|
print("New file path: %s " % self.file.path)
|
||||||
if os.path.exists(parent_full_path):
|
# Really move at the FS level
|
||||||
os.rename(settings.MEDIA_ROOT + old_path, settings.MEDIA_ROOT + new_path)
|
if os.path.exists(parent_full_path):
|
||||||
# Empty directories may remain, but that's not really a
|
os.rename(settings.MEDIA_ROOT + old_path, settings.MEDIA_ROOT + new_path)
|
||||||
# problem, and that can be solved with a simple shell
|
# Empty directories may remain, but that's not really a
|
||||||
# command: `find . -type d -empty -delete`
|
# problem, and that can be solved with a simple shell
|
||||||
|
# command: `find . -type d -empty -delete`
|
||||||
|
except Exception as e:
|
||||||
|
print("This file likely had a problem. Here is the exception:")
|
||||||
|
print(repr(e))
|
||||||
|
|
||||||
def _check_path_consistence(self):
|
def _check_path_consistence(self):
|
||||||
file_path = str(self.file)
|
file_path = str(self.file)
|
||||||
|
file_full_path = settings.MEDIA_ROOT + file_path
|
||||||
db_path = ".%s" % self.get_full_path()
|
db_path = ".%s" % self.get_full_path()
|
||||||
|
if not os.path.exists(file_full_path):
|
||||||
|
print("%s: WARNING: real file does not exists!" % self.id)
|
||||||
|
print("file path: %s" % file_path, end='')
|
||||||
|
print(" db path: %s" % db_path)
|
||||||
|
return False
|
||||||
if file_path != db_path:
|
if file_path != db_path:
|
||||||
print("%s: " % self.id, end='')
|
print("%s: " % self.id, end='')
|
||||||
print("file path: %s" % file_path, end='')
|
print("file path: %s" % file_path, end='')
|
||||||
|
Loading…
Reference in New Issue
Block a user