Beispiel #1
0
    def check_availability(self):
        """
        Perform check against Default Storage.
        """
        try:
            name = default_storage.get_valid_name('Informer Storage')

            # Save data.
            content = ContentFile('File used by StorageInformer checking.')
            path = default_storage.save(name, content)

            # Check properties.
            default_storage.size(path)
            default_storage.url(path)
            default_storage.path(path)

            default_storage.get_accessed_time(path)
            default_storage.get_available_name(path)
            default_storage.get_created_time(path)
            default_storage.get_modified_time(path)
            default_storage.get_valid_name(path)

            # And remove file.
            default_storage.delete(path)

            storage = default_storage.__class__.__name__
        except Exception as error:
            raise InformerException(
                f'An error occurred when trying to use your Storage: {error}')
        else:
            return True, f'Your {storage} is operational.'
Beispiel #2
0
    def get_context_data(self, **kwargs):

        documentversion_list = [{
            'name':
            d.uploaded_file.name,
            'generate_filename':
            default_storage.generate_filename(d.uploaded_file.name),
            'url':
            default_storage.url(
                default_storage.generate_filename(d.uploaded_file.name)),
            'exists':
            default_storage.exists(
                default_storage.generate_filename(d.uploaded_file.name)),
            'id':
            d.id
        } for d in DocumentVersion.objects.all()]

        kwargs.update({
            'objectstore_container_list': [
                'uploads/%s' % default_storage.get_valid_name(o)
                for o in default_storage.listdir('uploads')[1]
            ],
            'documentversion_list':
            documentversion_list,
        })
        return super().get_context_data(**kwargs)
Beispiel #3
0
    def check_availability(self):
        """
        Perform check against Default Storage.
        """
        try:
            name = default_storage.get_valid_name('Informer Storage')

            # Save data.
            content = ContentFile('File used by StorageInformer checking.')
            path = default_storage.save(name, content)

            # Check properties.
            default_storage.size(path)
            default_storage.url(path)
            default_storage.path(path)
            default_storage.modified_time(path)
            default_storage.created_time(path)

            # And remove file.
            default_storage.delete(path)

            storage = default_storage.__class__.__name__
        except Exception as error:
            raise InformerException(
                'A error occured when trying access your database: %s' % error)
        else:
            return True, 'Your %s is operational.' % storage
Beispiel #4
0
 def get_filename(self, filename):
     filename = default_storage.get_valid_name(os.path.basename(filename))
     filename = force_text(filename)
     filename = unicodedata.normalize('NFKD', filename).encode(
         'ascii', 'ignore').decode('ascii')
     name, ext = splitext(filename)
     print(f"name=[{name}] and ext=[{ext}]")
     return normpath(f"{self.document_type}{ext}")
Beispiel #5
0
def unique_filename(file):
    """
    Return the file's name as last component and \
    a unique ID as first component. \
    A unique ID is returned as filename if \
    the file's name is not valid. The extension \
    is assumed to be valid
    """
    name, ext = os.path.splitext(file.name)
    name = default_storage.get_valid_name(name)
    return os.path.join(
        safe_uuid(), '{name}{ext}'.format(name=name.lstrip('.') or safe_uuid(),
                                          ext=ext.lower()))
Beispiel #6
0
 def save_data(self, filename, django_file):
     """Save the response data from django_file.
     Does NOT save the StoredFile object, you must
     save it or the file will be forgotten and orphaned.
     django_file must be a django.core.files.File object (or subclass).
     filename is used to construct the actual filename in storage.
     """
     if self.file_exists():
         raise Exception("ERROR: Attempt to save respondent data that's already been saved")
     # We need to find a unique name - use a timestamp if we have to
     name = filename
     while default_storage.exists(name):
         name = default_storage.get_valid_name(u"%s_%s" % (filename, time.time()))
     self.filename = default_storage.save(name, django_file)
Beispiel #7
0
def download(image_url):
    response, content = client.request(image_url)
    
    #if response.status == 200 and response.get('content-type', None) == 'image/jpeg':
    if response.status == 200:
        image_url = image_url.split('?')[0] # chop the query string out
        image_name = default_storage.get_valid_name(image_url) 
        image_path = TWITTER_IMAGE_PATH + image_name
        if not default_storage.exists(image_path):
            log.info('saving image name = %s' % image_path)
            save_image(image_name, content)
        return image_name
    else:
        log.error('%s %s' % (response.status, response.get('content-type', None)))
        return None
Beispiel #8
0
def unique_filename(file):
    """
    Return the file's name as last component and \
    a unique ID as first component. \
    A unique ID is returned as filename if \
    the file's name is not valid. The extension \
    is assumed to be valid
    """
    name, ext = os.path.splitext(file.name)
    name = default_storage.get_valid_name(name)
    return os.path.join(
        safe_uuid(),
        '{name}{ext}'.format(
            name=name.lstrip('.') or safe_uuid(),
            ext=ext.lower()))
Beispiel #9
0
 def upload_image(self, request, *args, **kwargs):
     username = self.request.user.username
     serializer = self.get_serializer(data=request.data)
     if not serializer.is_valid():
         return Response(
             serializer.errors,
             status=status.HTTP_400_BAD_REQUEST,
         )
     file = request.data["file"]
     upload_file_name = default_storage.get_valid_name(file.name)
     upload_path = os.path.join("user_uploaded_file", f"{username}",
                                upload_file_name)
     saved_file = default_storage.save(upload_path, file)
     url = request.build_absolute_uri(default_storage.url(saved_file))
     data = {"name": saved_file, "url": url}
     return Response(data)
Beispiel #10
0
    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
        # This has two responsibilities:
        # 1. copy old template/static directories to themes/<pk>/*
        # 2. create a zipped version of those directories and save it to the
        #    Theme object.
        old_template = '{path}{dirname}/{pk}/'
        new_template = 'uploadtemplate/themes/{pk}/{dirname}/'
        dirnames = ('templates', 'static')
        upload_to = 'uploadtemplate/files/%Y/%m/%d'

        for theme in orm['uploadtemplate.Theme'].objects.all():
            sio = StringIO()
            zip_file = zipfile.ZipFile(sio, 'w')
            try:
                for dirname in dirnames:
                    format_kwargs = {'path': settings.UPLOADTEMPLATE_MEDIA_ROOT,
                                     'pk': theme.pk,
                                     'dirname': dirname}
                    old = old_template.format(**format_kwargs)
                    new = new_template.format(**format_kwargs)
                    for dir_path, dirs, files in os.walk(old):
                        for filename in files:
                            old_path = os.path.join(dir_path, filename)
                            name = old_path[len(old):]
                            new_path = os.path.join(new, name)
                            zip_path = os.path.join(dirname, name)
                            with open(old_path, 'r') as fp:
                                f = File(fp)
                                if default_storage.exists(new_path):
                                    default_storage.delete(new_path)
                                default_storage.save(new_path, f)
                            zip_file.write(old_path, zip_path)
            finally:
                zip_file.close()

            sio.seek(0)
            directory_name = os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(upload_to))))
            filename = os.path.normpath(default_storage.get_valid_name(os.path.basename('theme.zip')))
            name = os.path.join(directory_name, filename)
            name = default_storage.save(name, ContentFile(sio.read()))
            theme.theme_files_zip = name
            theme.save()
Beispiel #11
0
    def _save_attachments(self):
        # @@@ convert into proper form field with widget?
        delete_ids = set(self.data.getlist("remove-attachment"))
        for attachment in self.instance.attachments:
            if attachment.id in delete_ids:
                attachment.delete()

        # if we're saving as new version, bring forward existing attachments
        # from previous version
        prior_version = getattr(self, "prior_version", None)
        if prior_version is not None:
            for attachment in prior_version.attachments:
                self.instance.attachments.post(attachment)

        if not self.files:
            return
        for uf in self.files.getlist("attachment"):
            try:
                file_name = uf.name
                file_size = uf.size
            except AttributeError:
                continue

            if not file_name or not file_size:
                continue

            storage_name = default_storage.get_available_name(
                default_storage.get_valid_name(file_name))

            default_storage.save(storage_name, uf)

            attachment = Attachment(
                name=storage_name,
                description=file_name,
                url=default_storage.url(storage_name),
                size=file_size,
                attachmentType=AttachmentType.UNSPECIFIED
                )

            self.instance.attachments.post(attachment)
    def _save_attachments(self):
        # @@@ convert into proper form field with widget?
        delete_ids = set(self.data.getlist("remove-attachment"))
        for attachment in self.instance.attachments:
            if attachment.id in delete_ids:
                attachment.delete()

        # if we're saving as new version, bring forward existing attachments
        # from previous version
        if self.prior_version is not None:
            for attachment in self.prior_version.attachments:
                self.instance.attachments.post(attachment)

        if not self.files:
            return
        for uf in self.files.getlist("attachment"):
            try:
                file_name = uf.name
                file_size = uf.size
            except AttributeError:
                continue

            if not file_name or not file_size:
                continue

            storage_name = default_storage.get_available_name(
                default_storage.get_valid_name(file_name))

            default_storage.save(storage_name, uf)

            attachment = Attachment(name=storage_name,
                                    description=file_name,
                                    url=default_storage.url(storage_name),
                                    size=file_size,
                                    attachmentType=AttachmentType.UNSPECIFIED)

            self.instance.attachments.post(attachment)
Beispiel #13
0
 def get_valid_name(self, name):
     return default_storage.get_valid_name(name)
Beispiel #14
0
    def clean(self):
        cleaned_data = super(UploadMapForm, self).clean()

        file_obj = cleaned_data.get('file')
        if not file_obj:
            # no clean file => abort
            return cleaned_data

        # Make a save filename and copy the uploaded file
        saved_file = file_obj.temporary_file_path()
        safe_name = default_storage.get_valid_name(file_obj.name)
        tmpdir = tempfile.gettempdir()
        copied_file = shutil.copyfile(saved_file,
                                      os.path.join(tmpdir, safe_name))

        try:
            # call map info tool to generate minimap and json info file
            # change working directory so that the datadir is found
            old_cwd = os.getcwd()
            os.chdir(settings.WIDELANDS_SVN_DIR)
            check_call(['wl_map_info', copied_file])
            os.chdir(old_cwd)
        except CalledProcessError:
            self._errors['file'] = self.error_class(
                ['The map file could not be processed.'])
            del cleaned_data['file']
            os.remove(copied_file)
            return cleaned_data

        mapinfo = json.load(open(copied_file + '.json'))

        if Map.objects.filter(name=mapinfo['name']):
            self._errors['file'] = self.error_class(
                ['A map with the same name already exists.'])
            del cleaned_data['file']
            # Delete the file copy and the generated files
            try:
                os.remove(copied_file)
                os.remove(copied_file + '.json')
                os.remove(copied_file + '.png')
            except os.FileNotFoundError:
                pass

            return cleaned_data

        # Add information to the map
        self.instance.name = mapinfo['name']
        self.instance.author = mapinfo['author']
        self.instance.w = mapinfo['width']
        self.instance.h = mapinfo['height']
        self.instance.nr_players = mapinfo['nr_players']
        self.instance.descr = mapinfo['description']
        self.instance.hint = mapinfo['hint']
        self.instance.world_name = mapinfo['world_name']
        # The field is called 'wl_version_after' even though it actually means the
        # _minimum_ WL version required to play the map for historical reasons
        if 'minimum_required_widelands_version' in mapinfo:
            self.instance.wl_version_after = mapinfo[
                'minimum_required_widelands_version']
        else:
            self.instance.wl_version_after = 'build {}'.format(
                mapinfo['needs_widelands_version_after'] + 1)

        # mapinfo["minimap"] is the absolute path to the image file
        # We move the file to the correct destination
        minimap_name = mapinfo['minimap'].rpartition('/')[2]
        minimap_upload_to = self.instance._meta.get_field('minimap').upload_to
        minimap_path = os.path.join(minimap_upload_to, minimap_name)
        self.instance.minimap = minimap_path
        shutil.move(mapinfo['minimap'],
                    os.path.join(settings.MEDIA_ROOT, minimap_path))

        # Final cleanup
        os.remove(copied_file + '.json')
        os.remove(copied_file)

        return cleaned_data
Beispiel #15
0
 def get_filename(self, filename):
     filename = default_storage.get_valid_name(os.path.basename(filename))
     filename = force_text(filename)
     filename = unicodedata.normalize('NFKD', filename).encode(
         'ascii', 'ignore').decode('ascii')
     return os.path.normpath(filename)