Skip to content

Commit

Permalink
Remove hard coded MEDIA_ROOT
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed May 9, 2012
1 parent 498a433 commit 70cf7c2
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions chunked_uploads/models.py
Expand Up @@ -17,12 +17,18 @@
storage = None


def storage_path(obj, filename):
if isinstance(obj, Upload):
return os.path.join(obj.path_prefix(), filename).replace("/", "-")
# @@@ this replacement is a hack to work around bug in django-storages cloud files backend
# @@@ is this still necessary with cumulus?
return os.path.join(obj.upload.path_prefix(), "chunk")
STORAGE_PATH = getattr(settings, "CHUNKED_UPLOADS_STORAGE_PATH", None)
if STORAGE_PATH:
storage_path = STORAGE_PATH
else:
storage_path = lambda obj, fname: os.path.join("chunked_uploads", obj.uuid, fname)


CHUNKS_STORAGE_PATH = getattr(settings, "CHUNKED_UPLOADS_CHUNKS_STORAGE_PATH", None)
if CHUNKS_STORAGE_PATH:
chunks_storage_path = CHUNKS_STORAGE_PATH
else:
chunks_storage_path = lambda obj, fname: os.path.join("chunked_uploads", obj.upload.uuid, "chunks", "chunk")


class Upload(models.Model):
Expand Down Expand Up @@ -50,13 +56,12 @@ class Upload(models.Model):
def __unicode__(self):
return self.upload

def path_prefix(self):
s = str(self.uuid)
return os.path.join(s[:2], s[2:4], s[4:6], s)

def stitch_chunks(self):
fname = os.path.join(settings.MEDIA_ROOT, "tmp-" + storage_path(self, self.filename))
f = open(fname, "wb")
fname = os.path.join(
self.upload.storage.location,
storage_path(self, self.filename + ".tmp")
)
for chunk in self.chunks.all().order_by("pk"):
f.write(chunk.chunk.read())
f.close()
Expand All @@ -74,7 +79,7 @@ def uploaded_size(self):
class Chunk(models.Model):

upload = models.ForeignKey(Upload, related_name="chunks")
chunk = models.FileField(upload_to=storage_path)
chunk = models.FileField(upload_to=chunks_storage_path)
chunk_size = models.IntegerField()
created_at = models.DateTimeField(default=datetime.datetime.now)

Expand Down

0 comments on commit 70cf7c2

Please sign in to comment.