Hi folks,
I'm writing an application that takes multiple file uploads on a page. I
discovered that the naming of uploaded files is based on a timestamp,
and can only ensure unique filenames if no more than one file is
uploaded per second. In my case, this isn't true: I may be uploading two
or three files at time, in which case the first is overwritten by the
second, and the second is overwritten by the third.
I tackled the problem by adding a 'counter' iterator, that tacks a
unique number on the end of every filename. This should guarantee
uniqueness. Though it's not threadsafe; maybe I should have tossed the
thread id in there as well... ;-)
-- Graham
diff P:\Quixote-0.6b5\upload.py S:\quixote\upload.py
24a25,36
> class _counter:
> """
> Yield x for x in the set of naturals.
> A generator would be simpler, but reverse-incompatible.
> """
> def __init__(self):
> self.c = 0
> def next(self):
> self.c += 1
> return str(self.c)
>
> counter = _counter()
154,156c166,167
< tstamp = (strftime("%Y%m%d.%H%M%S", localtime(now)) +
< ("%.3f" % (now % 1))[1:])
< filename = "upload.%s.%s" % (tstamp, os.getpid())
---
> tstamp = strftime("%Y%m%d.%H%M%S", localtime(now))
> filename = "upload.%s.%s.%s" % (tstamp, os.getpid(),
counter.next())