Example #1
0
def save_file_to_storage(uploaded_file):
    """
    Saves an uploaded file to default storage and returns its filename.
    """
    storage = DefaultStorage()
    filename = storage.get_valid_name(uploaded_file.name)
    with storage.open(filename, 'wb') as f:
        for chunk in uploaded_file.chunks():
            f.write(chunk)
    return filename
Example #2
0
    def save(self):
        """Save the file and return the configuration.

        Returns:
            dict:
            The avatar service configuration.
        """
        storage = DefaultStorage()

        file_path = self.cleaned_data["avatar_upload"].name
        file_path = storage.get_valid_name(file_path)

        with storage.open(file_path, "wb") as f:
            f.write(self.cleaned_data["avatar_upload"].read())

        return {"absolute_url": storage.url(file_path), "file_path": file_path}
Example #3
0
    def save(self):
        """Save the file and return the configuration.

        Returns:
            dict:
            The avatar service configuration.
        """
        storage = DefaultStorage()

        file_path = self.cleaned_data['avatar_upload'].name
        file_path = storage.get_valid_name(file_path)

        with storage.open(file_path, 'wb') as f:
            f.write(self.cleaned_data['avatar_upload'].read())

        return {
            'absolute_url': storage.url(file_path),
            'file_path': file_path,
        }
Example #4
0
    def save(self):
        """Save the file and return the configuration.

        Returns:
            dict:
            The avatar service configuration.
        """
        storage = DefaultStorage()
        username = self.user.username

        uploaded_file = self.cleaned_data['avatar_upload']
        file_hash = md5()

        for chunk in uploaded_file.chunks():
            file_hash.update(chunk)

        # In the case where the filename does not have an extension,
        # splitext(filename) will return (filename, '').
        file_path = storage.get_valid_name(
            '%s%s'
            % (self.service.get_unique_filename(username),
               os.path.splitext(self.cleaned_data['avatar_upload'].name)[1])
        )

        file_path = os.path.join(username[0], username[:2], file_path)

        if self.service.file_path_prefix:
            file_path = os.path.join(self.service.file_path_prefix,
                                     file_path)

        file_path = storage.save(file_path, uploaded_file)

        return {
            'file_path': file_path,
            'file_hash': file_hash.hexdigest(),
        }