Esempio n. 1
0
def update_item(item, newname):
        file_root = os.path.join(settings.MEDIA_ROOT, get_storage_path(item, ''))
        path = item.file.path
        if not os.path.exists(path):
            print newname, ": files doesn't exist: ", path
            return
        root, name = os.path.split(path)
        base, ext = os.path.splitext(name)
        renamed = os.path.join(root, u'%s%s' % (newname, ext))

        cache = {}
        for size in MediaSizeCache().sizes.values():
            func = getattr(item, "get_%s_filename" % size.name, None)
            if func:
                sizename = func()
                if os.path.exists(sizename) and sizename.startswith(file_root):
                    cache[func] = sizename

        # Do the move and save
        move_file(item, path, renamed)
        # No need to regenerate caches
        item.prevent_cache_clear = True
        item.remove_deleted = False
        item.save()

        for key, value in cache.items():
            target = key()
            if not target.endswith('unconverted') and os.path.exists(value):
                file_move_safe(value, target)

        taken = item.date_taken
        taken = taken.astimezone(get_current_timezone())
        mtime = int(time.mktime(taken.timetuple()))
        os.utime(item.file.path, (mtime, mtime))
Esempio n. 2
0
def refresh_media():
    """
    Scan media upload folder and add any missing gallery items.
    """

    itemized = map(lambda o: o.file.path, MediaModel.objects.all())

    my_root = os.path.join(settings.MEDIA_ROOT, PHOTOLOGUE_DIR)
    for root, dirs, files in os.walk(my_root, followlinks=True):
        # First filter out cache and poster directories
        try:
            dirs.remove('cache')
            dirs.remove('poster')
        except:
            pass
        # Go througth files
        for fn in files:
            full = os.path.join(root, fn)
            if full in itemized:
                continue
            date_taken = datetime.fromtimestamp(os.path.getmtime(full))
            if not is_aware(date_taken):
                date_taken = make_aware(date_taken, get_current_timezone())

            # Next part is taken from process_zipfile
            filetype = False
            # Is it an image?
            try:
                trial_image = Image.open(full)
                trial_image.load()
                trial_image = Image.open(full)
                trial_image.verify()
                filetype = 'image'
            except Exception, e:
                pass
            # Is it a video?
            if not filetype:
                try:
                    sizes = video_info(full)
                    # Here comes a problem. If it is a jpeg image, it is
                    # detected as mjpeg movie...check if it least at least 1s long
                    if sizes[3] >= 1:
                        filetype = 'video'
                except Exception, e:
                    pass
            if not filetype:
                continue

            namebase, ext = os.path.splitext(fn)
            count = 0
            while 1:
                if count:
                    title = ''.join([namebase, '_'+str(count), ext])
                else:
                    title = fn
                slug = slugify(title)
                try:
                    p = GalleryItemBase.objects.get(title_slug=slug)
                except GalleryItemBase.DoesNotExist:
                    if filetype == 'image':
                        item = Photo(title=title,
                                  title_slug=slug)
                    elif filetype == 'video':
                        item = Video(title=title,
                                  title_slug=slug)
                    else:
                        raise Exception("Unknown file type")

                    # This will just update path in file entry 
                    move_file(item, full, full)
                    item.save()

                    if abs(item.date_taken - item.date_added) < timedelta(seconds=3):
                        item.date_taken = date_taken
                        item.save()
                    print "Added ", item.title, " - ", item.date_taken
                    break
                count = count + 1