Staging file are now indexed by a file content hash, fixed staging file rename on upload

This commit is contained in:
Roberto Rosario
2011-02-07 16:04:17 -04:00
parent 2ddab79ff5
commit 791742811f
3 changed files with 56 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
import errno
import os
import hashlib
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
@@ -9,6 +10,11 @@ from django.utils.translation import ugettext
from documents.conf.settings import STAGING_DIRECTORY
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
#func = lambda:[StagingFile.get_all() is None for i in range(100)]
#t1=time.time();func();t2=time.time();print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
def get_all_files():
try:
@@ -25,26 +31,24 @@ class StagingFile(object):
@classmethod
def get_all(cls):
staging_files = []
for id, filename in enumerate(get_all_files()):
for filename in get_all_files():
staging_files.append(StagingFile(
filepath=os.path.join(STAGING_DIRECTORY, filename),
id=id))
filepath=os.path.join(STAGING_DIRECTORY, filename)))
return staging_files
@classmethod
def get(cls, id):
files = get_all_files()
if id <= len(files):
return StagingFile(
filepath=os.path.join(STAGING_DIRECTORY, files[id]),
id=id)
raise ObjectDoesNotExist
files_dict = dict([(file.id, file) for file in cls.get_all()])
if id in files_dict:
return files_dict[id]
else:
raise ObjectDoesNotExist
def __init__(self, filepath, id):
def __init__(self, filepath):
self.filepath = filepath
self.filename = os.path.basename(filepath)
self._id = id
self._id = HASH_FUNCTION(open(filepath).read())
def __unicode__(self):
return self.filename