コード例 #1
0
    def zip(self, path, file=None, override=None, signal=False):
        """
        Zip file or directory ``path`` to ``file`` or to ``path + '.zip'``.
        On not exist raise :class:`~limited.core.files.storage.FileNotExist`.
        """
        path = self.check(path)
        if not self.fs.exists(path):
            raise FileNotExist(u"'%s' not found" % path)

        if file == None:
            file = self.available_name(path + u".zip", override=override)
        if signal:
            file_pre_change.send(self, basedir=FilePath.dirname(path))

        newfile = file + u".part"
        try:
            zfile = self.fs.open(newfile, mode='wb')
            archive = zipfile.ZipFile(zfile, 'w', zipfile.ZIP_DEFLATED)
            if self.fs.isdir(path):
                dirname = FilePath.name(path)
                for abspath, name in self.fs.listfiles(path).items():
                    name = FilePath.join(dirname, name)
                    archive.write(abspath, name)
            elif self.fs.isfile(path):
                archive.write(self.fs.abspath(path), FilePath.name(path))

            archive.close()
            zfile.seek(0)
            self.fs.rename(newfile, FilePath.name(file))
        except EnvironmentError as e:
            if e.errno == errno.EACCES:
                raise FileError(u"IOError, zip. Permission denied '%s'" % path)
        finally:
            if self.fs.exists(newfile):
                self.fs.remove(newfile)
コード例 #2
0
    def clear(self, path, older=None, signal=True):
        """
        Remove all files and dirs in ``path`` directory.
        ``older`` takes seconds for max age from created_time, only top sub dirs checked.
        On not exist raise :class:`~limited.core.files.storage.FileNotExist`.
        On not directory raise :class:`~limited.core.files.storage.FileError`.
        """
        path = self.check(path)
        if signal:
            file_pre_change.send(self, basedir=FilePath.dirname(path))

        if not self.fs.exists(path):
            raise FileNotExist(u"'%s' not found" % path)
        if not self.fs.isdir(path):
            raise FileError(u"'%s' not directory" % path)
        if signal:
            file_pre_change.send(self, basedir=FilePath.dirname(path))
        if older == None:
            for item in self.fs.list(path):
                file = FilePath.join(path, item)
                self.fs.remove(file)
        else:
            for item in self.fs.list(path):
                file = FilePath.join(path, item)
                chenaged = self.fs.created_time(file)
                if datetime.now() - chenaged > timedelta(seconds=older):
                    self.fs.remove(file)
コード例 #3
0
    def build(self, path):
        """
        Return HttpResponse object with file
        or instruction to http server where file exists.
        Method don't check data size or anything else,
        archiving running for all data in main thread.
        So you have to check it manually with ``is_need_processing``
        """
        url_path = None
        if self.storage.isdir(path):
            if not self.storage.exists(settings.LIMITED_CACHE_PATH):
                self.storage.mkdir(settings.LIMITED_CACHE_PATH)
            if not self.storage.exists(self.cache_file(path)):
                self.storage.extra.zip(path, self.cache_file(path))
            url_path = self.cache_file(path)
            name = FilePath.name(path) + '.zip'

        elif self.storage.isfile(path):
            url_path = path
            name = FilePath.name(path)
        else:
            raise FileNotExist(u"'%s' not found" % path)

        Backend = self.get_backend()
        Response = Backend(self.storage, settings.LIMITED_SERVE)
        response = Response.generate(url_path, name)

        return response
コード例 #4
0
ファイル: __init__.py プロジェクト: b7w/limited-fm
 def move(self, src, dst, signal=True):
     """
     Move file or dir from ``src`` to ``dst``.
     On the same directory raise :class:`~limited.core.files.storage.FileError`.
     On not exist for both paths raise :class:`~limited.core.files.storage.FileNotExist`.
     """
     src_dir = FilePath.dirname(src)
     if src == dst or src_dir == dst:
         raise FileError(u"Moving to the same directory")
     if not self.exists(src):
         raise FileNotExist(u"'%s' not found" % src)
     if not self.exists(dst):
         raise FileNotExist(u"'%s' not found" % dst)
     src = self.check(src)
     dst = self.check(dst)
     if signal:
         file_pre_change.send(self, basedir=FilePath.dirname(src))
         file_pre_change.send(self, basedir=dst)
     self.fs.move(src, dst)
コード例 #5
0
ファイル: __init__.py プロジェクト: b7w/limited-fm
 def remove(self, path, signal=True):
     """
     Remove directory or file, on not exist raise :class:`~limited.core.files.storage.FileNotExist`
     """
     path = self.check(path)
     if not self.exists(path):
         raise FileNotExist(u"'%s' not found" % path)
     if signal:
         file_pre_change.send(self, basedir=FilePath.dirname(path))
     self.fs.remove(path)
コード例 #6
0
 def totrash(self, path, signal=True):
     """
     Shortcut for :func:`~limited.core.files.storage.FileStorage.move`
     where second var is :ref:`LIMITED_TRASH_PATH <SETTINGS_TRASH_PATH>`.
     """
     path = self.check(path)
     if signal:
         file_pre_change.send(self, basedir=FilePath.dirname(path))
     if not self.fs.exists(settings.LIMITED_TRASH_PATH):
         self.fs.mkdir(settings.LIMITED_TRASH_PATH)
     if not self.fs.exists(path):
         raise FileNotExist(u"'%s' not found" % path)
     self.fs.move(path, settings.LIMITED_TRASH_PATH)
コード例 #7
0
 def check(path):
     """
     Return norm path or raise FileError
     if path is hidden raise FileNotExist
     """
     path = FilePath.norm( path )
     if not FilePath.check(path):
         raise FileError( u"IOError, Permission denied" )
         #TODO: TRASH and CACHE are visible, is is not good.
     if path.startswith( u'.' ) or u'/.' in path:
         if settings.LIMITED_TRASH_PATH not in path and settings.LIMITED_CACHE_PATH not in path:
             raise FileNotExist( u"path '%s' doesn't exist or it isn't a directory" % path )
     return path
コード例 #8
0
    def unzip(self, path, override=True, signal=False):
        """
        Unzip file or directory ``path``.
        On not exist raise :class:`~limited.core.files.storage.FileNotExist`.
        """
        path = self.check(path)
        if not self.fs.exists(path):
            raise FileNotExist(u"'%s' not found" % path)

        file = self.fs.abspath(path)
        directory = FilePath.dirname(path)
        # To lazy to do converting
        # maybe chardet help later
        if signal:
            file_pre_change.send(self, basedir=FilePath.dirname(path))
        zip = None
        try:
            zip = zipfile.ZipFile(file, 'r')

            for name in zip.namelist():
                try:
                    unicode_name = unicode(name)
                except UnicodeDecodeError:
                    unicode_name = unicode(name.decode('cp866'))

                full_path = FilePath.join(directory, unicode_name)
                dir_path = FilePath.dirname(unicode_name)
                if dir_path != '':
                    tmp = directory
                    for item in FilePath.split(dir_path):
                        tmp = FilePath.join(tmp, item)
                        if not self.fs.exists(tmp):
                            self.fs.mkdir(tmp)

                if not unicode_name.endswith('/'):
                    with self.fs.open(full_path, 'w') as f:
                        f.write(zip.open(name).read())

        except UnicodeDecodeError as e:
            raise FileError(u"Unicode decode error '%s', try unzip yourself" %
                            path)
        except zipfile.BadZipfile as e:
            raise FileError(u"Bad zip file '%s'" % path)
        except EnvironmentError as e:
            if e.errno == errno.EACCES:
                raise FileError(u"IOError, unzip. Permission denied '%s'" %
                                path)
        finally:
            if zip:
                zip.close()
コード例 #9
0
ファイル: __init__.py プロジェクト: b7w/limited-fm
    def rename(self, path, name, signal=True):
        """
        Rename file or dir path ``path`` to name ``name``.
        On '/' in ``name`` raise :class:`~limited.core.files.storage.FileError`.
        On not exist or already exist raise :class:`~limited.core.files.storage.FileNotExist`.
        """
        path = self.check(path)
        if '/' in name:
            raise FileError(u"'%s' contains not supported symbols" % name)
        if not self.exists(path):
            raise FileNotExist(u"'%s' not found" % path)

        new_path = FilePath.join(FilePath.dirname(path), name)
        if self.exists(new_path):
            raise FileError(u"'%s' already exist!" % name)
        if signal:
            file_pre_change.send(self, basedir=FilePath.dirname(path))
        self.fs.rename(path, name)
コード例 #10
0
ファイル: views.py プロジェクト: b7w/limited-fm
def ResizeView(request, id, size):
    """
    Resize view 
    """
    if request.user.is_anonymous() and not settings.LIMITED_ANONYMOUS:
        return HttpResponseRedirect('%s?next=%s' %
                                    (settings.LOGIN_URL, request.path))

    if request.method == u"GET":
        lib_id = int(id)
        path = request.GET.get('p', '')
        if FilePath.check(path, norm=True) == False:
            logger.error(
                u"Files. Path check fail. home_id:{0}, path:{1}".format(
                    lib_id, path))
            return RenderError(request, u"IOError, Permission denied")

        try:
            options = ResizeOptions(size)
            home = get_home(request.user, lib_id)
            storage = home.lib.getStorage()
            if not storage.isfile(path):
                raise FileNotExist()
            tmp = path.lower()
            if not (tmp.endswith(".jpg") or tmp.endswith(".jpeg")):
                raise FileNotExist()

            HashBuilder = FileUnicName()
            hash_path = HashBuilder.build(path, extra=options.size)
            hash_path = FilePath.join(settings.LIMITED_CACHE_PATH,
                                      hash_path + ".jpg")
            bigger = False
            if not storage.exists(settings.LIMITED_CACHE_PATH):
                storage.mkdir(settings.LIMITED_CACHE_PATH)
            if not storage.exists(hash_path):
                filein = storage.open(path, mode='rb', signal=False)
                newImage = ResizeImage(filein)
                bigger = newImage.isBigger(options.width, options.height)
                if not bigger:
                    if options.crop:
                        w, h = newImage.minSize(options.size)
                        newImage.resize(w, h)
                        newImage.cropCenter(options.width, options.height)
                    else:
                        w, h = newImage.maxSize(options.size)
                        newImage.resize(w, h)
                    fileout = storage.open(hash_path, mode='wb', signal=False)
                    newImage.saveTo(fileout)
                    fileout.close()
                filein.close()
            manager = DownloadManager(home.lib)
            if not bigger:
                response = manager.build(hash_path)
            else:
                response = manager.build(path)

        except ResizeOptionsError:
            raise Http404(u"Wrong resize option")
        except ObjectDoesNotExist:
            logger.error(
                u"ResizeView. No such file lib or you don't have permissions. home_id:{0}, path:{1}"
                .format(lib_id, path))
            return RenderError(
                request, u"No such file lib or you don't have permissions")
        except FileNotExist as e:
            logger.error(
                u"ResizeView. No file or directory find. home_id:{0}, path:{1}"
                .format(lib_id, path))
            raise Http404(u"No file or directory find")

        return response