Esempio n. 1
0
    def get_direct_link(self, dbx_file):
        hash_key = 'direct_link@'+str(g.author_uid)+'@'+_md5(dbx_file.path)+'@'+dbx_file.rev
        
        content = self.storage.get(hash_key)
        if content:
            # if cached link exist and it expires after 1 hour: re-use it.
            tokens = content.split('|')
            expires = datetime_from_dropbox(tokens[1])

            td =  expires - datetime.utcnow()
            total_seconds = (td.seconds + td.days * 24 * 3600)
            if total_seconds > 3600:
                return tokens[0]

        media = g.author.dropbox_client.media(dbx_file.path)
        url = urllib.unquote(media['url'])
        expires = media['expires']

        self.storage.set(hash_key, url+'|'+expires)

        return url
Esempio n. 2
0
File: dbx.py Progetto: d5/daniel.gs
 def modified(self):
     if self._modified:
         return datetime_from_dropbox(self._modified)
     else:
         return datetime.min
Esempio n. 3
0
File: dbx.py Progetto: d5/daniel.gs
    def get_files(self, patterns="*", index=0, count=-1, sort_key=None, sort_reverse=False, excludes=[]):
        if not self.is_dir:
            raise Exception("The path is not a directory: " + self.path)

        if index < 0:
            index = 0
        if count < 0:
            count = 1

        if not isinstance(patterns, list):
            patterns = [patterns]

        if not isinstance(excludes, list):
            excludes = [excludes]

        # change all patterns to lower-case
        skip_pattern_test = False
        for i in xrange(len(patterns)):
            patterns[i] = patterns[i].lower()
            if patterns[i] == "*":
                skip_pattern_test = True
        for i in xrange(len(excludes)):
            excludes[i] = excludes[i].lower()

        # apply filter based on patterns
        files = []
        for f, c in self._files.items():
            # exclude dir entries
            if c.get("is_dir", False):
                continue

            # test against exclude patterns
            exc = False
            for e in excludes:
                if fnmatch(f, e):
                    exc = True
                    break
            if exc:
                continue

            # test against patterns
            if skip_pattern_test:
                files.append(c)
            else:
                for p in patterns:
                    if fnmatch(f, p):
                        files.append(c)
                        break

        # sort files
        if sort_key == "modified":
            # sorting by modified time
            files.sort(key=lambda x: datetime_from_dropbox(x[sort_key]), reverse=sort_reverse)
        elif sort_key:
            # sorting by other key
            files.sort(key=lambda x: x[sort_key].lower(), reverse=sort_reverse)

        # enumerate
        if len(files):
            # adjust 'index' and 'count'
            filtered_count = len(files)
            if index >= filtered_count:
                index = filtered_count - 1
            if index + count > filtered_count:
                count = filtered_count - index

            return [self.get_file(get_file_name(c["path"])) for c in files[index : index + count]]
        else:
            return []