Beispiel #1
0
    def chown(self, uid, gid):
        ''' Change ownership of this file and its two immediate ancestors. '''

        file_path = os.path.join(get_path('data'), self.relpath())
        os.chown(file_path, uid, gid)

        ancestor1_path = os.path.dirname(file_path)
        os.chown(ancestor1_path, uid, gid)

        ancestor2_path = os.path.dirname(ancestor1_path)
        os.chown(ancestor2_path, uid, gid)
Beispiel #2
0
    def __init__(self,
                 name,
                 mime,
                 user_id,
                 content=None,
                 zip_archive=False,
                 zip_files=[],
                 zip_str_files=[],
                 access_type='private'):
        '''
        Constructor.
        '''

        self.name = name
        self.mime = mime
        self.user_id = user_id
        self.access_type = access_type

        # Create dummy content to use in hash if there is
        # no content (zip archives)
        if content is None:
            content = ('DUMMY DATA - {}' + random_string(1000)).encode('utf8')

        hash_ = hashlib.sha256()
        hash_.update(content)
        self.hash = hash_.digest()

        # Write content to file.
        data_dir = get_path('data')
        hash_hex = binascii.hexlify(self.hash).decode('ascii')
        dir1 = os.path.join(data_dir, hash_hex[0])
        dir2 = os.path.join(dir1, hash_hex[1])
        path = os.path.join(dir2, hash_hex[2:])

        if not os.path.isdir(dir1):
            os.mkdir(dir1)

        if not os.path.isdir(dir2):
            os.mkdir(dir2)

        if not os.path.isfile(path):
            if zip_archive:
                self.zip_files(path, zip_files, zip_str_files)
            else:
                file_ = open(path, 'wb')
                file_.write(content)
                file_.close()
Beispiel #3
0
    def thumb_data(self):
        '''
        Get user thumbnail in base64 encoding.

        Returns a default thumbnail if this user does not have one.
        '''

        if self.thumb is None:
            if User.DEFAULT_THUMB is None:
                dir_ = get_path('static/img')
                file_ = 'default_user_thumb.png'

                with open(os.path.join(dir_, file_), 'rb') as fh:
                    User.DEFAULT_THUMB = fh.read()

            thumb_data = User.DEFAULT_THUMB
        else:
            thumb_data = self.thumb

        return base64.b64encode(thumb_data).decode('ascii')
Beispiel #4
0
    def zip_files(self, path, files, str_files):
        ''' Create a zip archive of files and string files.'''

        zip_file = zipfile.ZipFile(path, 'w')
        data_dir = get_path('data')

        # Add files
        for f in files:
            f_path = os.path.join(data_dir, f[1])
            zip_file.write(f_path, arcname=f[0], compress_type=zipfile.ZIP_DEFLATED)

        # Write string files
        for str_file in str_files:
            info = zipfile.ZipInfo(str_file[0])
            info.date_time = time.localtime(time.time())[:6]
            info.compress_type = zipfile.ZIP_DEFLATED
            # http://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zip/6297838#6297838
            info.external_attr = 0o644 << 16 # rw-r-r
            zip_file.writestr(info, str_file[1])

        zip_file.close()