def upload(request):
    context = {}
    if request.method == 'POST':
        uploaded_file = request.FILES['document']
        fs = FileSystemStorage()
        name = fs.save(uploaded_file.name, uploaded_file)
        context['url'] = fs.url(name)
        with open(os.path.join(STATICFILES_DIRS[0], 'JSON', 'url.json'),
                  'w') as json_file:
            json.dump({'url': context['url']}, json_file)
        analysisAPI.start_analysis(fs.get_valid_name(name))
        file = open(os.path.join(STATICFILES_DIRS[0], 'JSON', 'result.json'))
        result = file.read()
        file.close()
        with open(os.path.join(STATICFILES_DIRS[0], 'JSON',
                               'result.json')) as f:
            res1 = json.load(f)
        return render(
            request, 'analysis/upload.html', {
                'result': result,
                'url': context['url'],
                'alltext': str(res1['allText']),
                'finTones': res1['finTones']
            })
    return render(request, 'analysis/upload.html')
Пример #2
0
def upload(request):
    if request.method == 'POST':
        
        f = request.FILES.get('file')
        
        resp = {}
        
        fs = FileSystemStorage(location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL)
        
        filename = fs.get_valid_name(f.name)
        #Save the file to disk
        disk_path = fs.save(filename, f)
        #Save a thumbnail to disk
        width, height = None, None
        try:
            # Thumbnails are saved as JPG unless the source is PNG. Then it's saved as a PNG to preserve transparency.
            suffix = '-thumb.jpg'
            if f.content_type == 'image/png':
                suffix = '-thumb.png'
            if f.content_type == 'image/gif':
                suffix = '-thumb.gif'
            thumbnail_filename = filename.split('.')[0] + suffix
            im = Image.open(os.path.join(settings.MEDIA_ROOT, filename))
            width, height = im.size
            im.thumbnail((140, 140), Image.ANTIALIAS)
            im.save(os.path.join(settings.MEDIA_ROOT, thumbnail_filename))
        except IOError:
            thumbnail_filename = None
            pass
        #Store reference to the file in our model
        mediaAsset = models.MediaAsset()
        mediaAsset.filename = filename
        mediaAsset.base_url = '{ MEDIA_URL }'
        mediaAsset.size = fs.size(filename)
        mediaAsset.type = f.content_type
        if thumbnail_filename:
            mediaAsset.thumbnail = thumbnail_filename
            resp['thumbnail_url'] = fs.url(thumbnail_filename)
        if width and height:
            mediaAsset.width = width
            mediaAsset.height = height
            resp['width'] = width
            resp['height'] = height
        mediaAsset.save()
        
        resp['filename'] = filename
        resp['id'] = mediaAsset.id
        resp['url'] = fs.url(filename)
        resp['size'] = fs.size(filename)
        
        return HttpResponse(json.dumps(resp), content_type="text/json")
    return HttpResponseNotAllowed(permitted_methods=['POST'])
Пример #3
0
def simple_upload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        name = 'test-case.xlsm'
        file_name = fs.get_valid_name('test-case.xlsm')
        if (file_name == name):
            fs.delete(file_name)
        filename = fs.save('test-case.xlsm', myfile)
        uploaded_file_url = fs.url(filename)

    context = {
        'uploaded_file_url': uploaded_file_url,
    }

    return render(request, 'Prediction/dbupdate.html', context)
Пример #4
0
    def create_and_save_image(self, user, image, content_type, object_id):
        # Use Django's get_valid_name() to get a safe filename
        storage = FileSystemStorage()
        safe_filename = storage.get_valid_name(image.name)
        im = self.create(content_type=content_type,
                         object_id=object_id,
                         user=user,
                         revision=1,
                         name=image.name)
        path = '%swlimages/%s' % (settings.MEDIA_ROOT, safe_filename)

        destination = open(path, 'wb')
        for chunk in image.chunks():
            destination.write(chunk)

        im.image = 'wlimages/%s' % (safe_filename)

        im.save()
Пример #5
0
class ExhibitStorage(FileSystemStorage):
    """
    Assume that incoming paths are of the form <username>/.../...
    """
    def __init__(self, *args, **kwargs): 
        self.__userdata_storage = FileSystemStorage(location=kwargs['location'], base_url=kwargs['base_url'])
        
    def _open(self, name, mode='rb'):
        return self.__userdata_storage._open(chunk_path(name) + name, mode)
        
    def _save(self, name, content):
        chunk = chunk_path(name)       
        fullname = chunk + name 
        if (self.__userdata_storage.exists(fullname)):
            self.__userdata_storage.delete(fullname)
        result = self.__userdata_storage._save(fullname, content)
        return result.partition(chunk)[2]
        
    def exists(self, name):
        return self.__userdata_storage.exists(chunk_path(name) + name)
    
    def path(self, name):
        return self.__userdata_storage.path(chunk_path(name) + name)
    
    def size(self, name):
        return self.__userdata_storage.size(chunk_path(name) + name)
    
    def delete(self, name):
        return self.__userdata_storage.delete(chunk_path(name) + name)
    
    def url(self, name):
        return self.__userdata_storage.url(name)
    def get_available_name(self, name):
        return self.__userdata_storage.get_available_name(name)
    def get_valid_name(self, name):
        return self.__userdata_storage.get_valid_name(name)
Пример #6
0
    def process_form_submission(self, request, form):
        """
        Accepts form instance with submitted data, user and page.
        Creates submission instance.

        You can override this method if you want to have custom creation logic.
        For example, if you want to save reference to a user.
        """
        processed_data = {}

        # Handle file uploads
        for key, val in form.cleaned_data.items():
            if type(val) == InMemoryUploadedFile or type(val) == TemporaryUploadedFile:
                # Save the file and get its URL
                file_system = FileSystemStorage(
                    location=cr_settings['PROTECTED_MEDIA_ROOT'],
                    base_url=cr_settings['PROTECTED_MEDIA_URL']
                )
                filename = file_system.save(file_system.get_valid_name(val.name), val)
                processed_data[key] = file_system.url(filename)
            else:
                processed_data[key] = val

        # Get submission
        form_submission = self.get_submission_class()(
            form_data=json.dumps(processed_data, cls=DjangoJSONEncoder),
            page=self,
        )

        # Save to database
        if self.save_to_database:
            form_submission.save()

        # Send the mails
        if self.to_address:
            self.send_summary_mail(request, form, processed_data)

        if self.confirmation_emails:
            for email in self.confirmation_emails.all():
                from_address = email.from_address

                if from_address == '':
                    from_address = GeneralSettings.for_site(request.site).from_email_address

                template_body = Template(email.body)
                template_to = Template(email.to_address)
                template_from_email = Template(from_address)
                template_cc = Template(email.cc_address)
                template_bcc = Template(email.bcc_address)
                template_subject = Template(email.subject)
                context = Context(self.data_to_dict(processed_data))

                message = EmailMessage(
                    body=template_body.render(context),
                    to=template_to.render(context).split(','),
                    from_email=template_from_email.render(context),
                    cc=template_cc.render(context).split(','),
                    bcc=template_bcc.render(context).split(','),
                    subject=template_subject.render(context),
                )

                message.content_subtype = 'html'
                message.send()

        for fn in hooks.get_hooks('form_page_submit'):
            fn(instance=self, form_submission=form_submission)

        return processed_data
Пример #7
0
class FileManager(object):
    current_user = None
    user_directory = None
    user_trash = None
    user_storage = None
    trash_storage = None

    def __init__(self, user=None):
        self.current_user = user
        if self.current_user != None:
            self.user_directory = settings.MEDIA_ROOT + "/" + self.current_user.user_id
            self.user_trash = settings.TRASH_ROOT + "/" + self.current_user.user_id
            # For security, limit access to user directory only
            self.user_storage = FileSystemStorage(location=self.user_directory)
            self.trash_storage = FileSystemStorage(location=self.user_trash)
        else:
            # Handle none logged in user
            user_storage = None
        self.update_path(None)

    def set_share_path(self, path):
        self.user_directory = settings.MEDIA_ROOT + "/" + self.current_user.user_id
        self.user_trash = settings.TRASH_ROOT + "/" + self.current_user.user_id
        self.user_storage = FileSystemStorage(location=self.user_directory)
        self.trash_storage = FileSystemStorage(location=self.user_trash)
        if os.path.isfile(self.user_storage.path(path)):
            return 1  # File
        else:
            self.update_path(path)
            return 0  # Directory

    def update_path(self, path):
        if path is None or len(path) == 0:
            self.path = ''
            self.abspath = self.user_storage.path(self.user_directory)
        else:
            self.path = self.clean_path(path)
            self.abspath = os.path.join(
                self.user_storage.path(self.user_directory), self.path)
        self.location = self.abspath
        # self.url = os.path.join(settings.MEDIA_URL, self.path)

    def clean_path(self, path):
        # replace backslash with slash
        path = path.replace('\\', '/')
        # remove leading and trailing slashes
        path = '/'.join([i for i in path.split('/') if i])

        return path

    def get_breadcrumbs(self):
        breadcrumbs = [{
            'label': 'fm-home',
            'path': '',
        }]

        parts = [e for e in self.path.split('/') if e]

        path = ''
        for part in parts:
            path = os.path.join(path, part)
            breadcrumbs.append({
                'label': part,
                'path': path,
            })

        return breadcrumbs

    def update_context_data(self, context):
        context.update({
            'path': self.path,
            'breadcrumbs': self.get_breadcrumbs(),
        })

    def file_details(self, file_path):
        file_path = self.user_storage.path(file_path)
        filename = smart_str(os.path.split(file_path)[1])  # Extract filemame
        if os.path.isfile(file_path):
            return {
                'md5checksum': md5_checksum(file_path),
                'directory': os.path.dirname(file_path),
                'filename': filename,
                'filesize':
                file_size_formatted(self.user_storage.size(file_path)),
                'filedate': self.user_storage.get_modified_time(file_path),
                'fileurl': file_path,
            }
        else:
            return {}

    def directory_list(self, include_files=True):
        listing = []

        directories, files = self.user_storage.listdir(self.location)

        def _helper(name, filetype):
            return {
                'filepath':
                os.path.join(self.path, name),
                'filetype':
                filetype,
                'filename':
                name,
                'filedate':
                self.user_storage.get_modified_time(
                    os.path.join(self.path, name)),
                'filesize':
                file_size_formatted(
                    self.user_storage.size(os.path.join(self.path, name))),
            }

        for directoryname in directories:
            listing.append(_helper(directoryname, 'directory'))

        if include_files:
            mimetypes.init()
            for filename in files:
                guessed_mime = mimetypes.guess_type(filename)[0]
                if (guessed_mime == None):
                    file_mime = "unknown"
                else:
                    file_mime = str(guessed_mime)
                listing.append(_helper(filename, file_mime))

        return listing

    def trash_list(self):
        listing = []

        directories, files = self.trash_storage.listdir(self.user_trash)

        def _helper(name, filetype):
            return {
                'filepath':
                self.trash_storage.get_valid_name(name),
                'filetype':
                filetype,
                'filename':
                name,
                'filedate':
                self.trash_storage.get_modified_time(
                    os.path.join(self.user_trash, name)),
                'filesize':
                file_size_formatted(
                    self.trash_storage.size(os.path.join(
                        self.user_trash, name))),
            }

        for directoryname in directories:
            listing.append(_helper(directoryname, 'directory'))

        mimetypes.init()
        for filename in files:
            guessed_mime = mimetypes.guess_type(filename)[0]
            if (guessed_mime == None):
                file_mime = "unknown"
            else:
                file_mime = str(guessed_mime)
            listing.append(_helper(filename, file_mime))

        return listing

    def delete_item(self, item_path):
        res_path = item_path
        i = 0
        try:
            delete_path = os.path.join(
                self.user_storage.path(self.user_directory), item_path)
            while os.path.exists(
                    self.trash_storage.path(os.path.basename(res_path))):
                i = i + 1
                res_path = str(i) + "_" + os.path.basename(item_path)
                new_delete_path = os.path.join(
                    self.user_storage.path(self.user_directory), res_path)
                os.rename(delete_path, new_delete_path)
                delete_path = new_delete_path
        except Exception:
            return HttpResponseNotFound("File not found")
        # Move to trash
        shutil.move(delete_path, self.trash_storage.path(self.user_trash))
        # Unshare item if it is shared
        if ShareUrl.objects.filter(owner=self.current_user,
                                   path=item_path).exists():
            share_url = get_object_or_404(ShareUrl,
                                          owner=self.current_user,
                                          path=item_path)
            share_url.delete()
        return JsonResponse({'result': 0})

    def purge_item(self, item_path):
        try:
            delete_path = os.path.join(
                self.trash_storage.path(self.user_trash), item_path)
        except Exception:
            return HttpResponseNotFound("File not found")
        # Permanantly delete file
        total_size = 0
        if os.path.isdir(delete_path):
            # Get the size of the file
            for path, dirs, files in os.walk(delete_path):
                for f in files:
                    fp = os.path.join(path, f)
                    total_size += os.path.getsize(fp)
                for fold in dirs:
                    total_size += 4096
            shutil.rmtree(delete_path,
                          ignore_errors=True)  # Delete selected directory
            # Update Quota
            user_db = get_object_or_404(User, pk=self.current_user.user_id)
            user_db.used_quota = user_db.used_quota - int(total_size)
            user_db.save()
        else:
            total_size = os.path.getsize(delete_path)
            os.remove(delete_path)  # Delete file
            # Update Quota
            user_db = get_object_or_404(User, pk=self.current_user.user_id)
            user_db.used_quota = user_db.used_quota - int(total_size)
            user_db.save()
        return JsonResponse({'result': 0})

    def upload_file(self, file_data):
        filename = self.user_storage.get_valid_name(file_data.name)
        upload_path = os.path.join(self.user_storage.path(self.location),
                                   filename)
        # Check if user has sufficient space
        if file_data.size > self.current_user.get_remaining_quota():
            # Insufficent space
            return False
        else:
            if os.path.exists(self.user_storage.path(upload_path)):
                # Overwrite existing file and remove from quota
                user_db = get_object_or_404(User, pk=self.current_user.user_id)
                user_db.used_quota = user_db.used_quota - int(
                    os.path.getsize(self.user_storage.path(upload_path)))
                user_db.save()
                os.remove(self.user_storage.path(upload_path))
            try:
                # Set max_length as a safety precaution to not go over-quota
                self.user_storage.save(
                    upload_path,
                    file_data,
                    max_length=self.current_user.get_remaining_quota())
                user_db = get_object_or_404(User, pk=self.current_user.user_id)
                # Update Quota
                user_db.used_quota = user_db.used_quota + int(file_data.size)
                user_db.save()
                return True
            except Exception as ex:
                # Upload failed. Not enough space
                return False

    def empty_trash(self):
        # Delete trsah folder and recreate it
        delete_path = self.user_trash
        # Get the size of the trash
        total_size = 0
        for path, dirs, files in os.walk(delete_path):
            for f in files:
                fp = os.path.join(path, f)
                total_size += os.path.getsize(fp)
            for fold in dirs:
                total_size += 4096
        shutil.rmtree(delete_path,
                      ignore_errors=True)  # Delete selected directory
        os.mkdir(delete_path)
        # Update Quota
        user_db = get_object_or_404(User, pk=self.current_user.user_id)
        user_db.used_quota = user_db.used_quota - int(total_size)
        user_db.save()
        return JsonResponse({'result': 0})

    def restore_item(self, item_path):
        res_path = item_path
        i = 0
        try:
            restore_path = os.path.join(
                self.trash_storage.path(self.user_trash), item_path)
            user_dir_path = os.path.join(
                self.user_storage.path(self.user_directory))
            # Rename if item already exists
            while os.path.exists(self.user_storage.path(res_path)):
                i = i + 1
                res_path = str(i) + "_" + item_path
                new_restore_path = os.path.join(
                    self.trash_storage.path(self.user_trash), res_path)
                os.rename(restore_path, new_restore_path)
                restore_path = new_restore_path
        except Exception:
            return HttpResponseNotFound("File not found")
        # Move item back to user directory
        res_location = shutil.move(restore_path, user_dir_path)
        return JsonResponse({
            'result':
            0,
            'location':
            res_location.replace(self.user_storage.path(""), "")
        })

    def download_file(self, filename):
        download_path = ''
        try:
            download_path = os.path.join(
                self.user_storage.path(self.user_directory), filename)
        except Exception:
            return HttpResponseNotFound("File not found")
        if os.path.isdir(download_path):
            # Cannot download directory
            return HttpResponseForbidden("Not allowed")
        else:
            file_wrapper = FileWrapper(open(download_path, 'rb'))
            file_mimetype = mimetypes.guess_type(download_path)[0]
            response = HttpResponse(file_wrapper, content_type=file_mimetype)
            response['X-Sendfile'] = download_path
            response['Content-Length'] = self.user_storage.size(download_path)
            # Extract filename only
            response[
                'Content-Disposition'] = 'attachment; filename=%s' % smart_str(
                    os.path.split(filename)[1])
            return response

    def create_directory(self, dir_name):
        """
		Create directory by creating temp file in the directory
		FileStorageAPI will create the directory while saving the 
		empty temp file then delete the temp file leaving behind
		the empty new directory
		"""
        new_directory = os.path.join(self.location, dir_name)
        if os.path.exists(self.user_storage.path(new_directory)):
            # Directory already exists
            return False
        else:
            new_path = self.user_storage.path(new_directory)
            temp_file = os.path.join(new_path, '.tmp')
            self.user_storage.save(temp_file, ContentFile(''))
            self.user_storage.delete(temp_file)
            user_db = get_object_or_404(User, pk=self.current_user.user_id)
            # Update Quota - 4KB per directory
            user_db.used_quota = user_db.used_quota + int(4096)
            user_db.save()
            return True

    def move(self, old_path, new_path):
        """
		Moves a given file to the provided destination
		"""
        current_path = os.path.join(
            self.user_storage.path(self.user_directory), old_path)
        move_path = os.path.join(self.user_storage.path(self.user_directory),
                                 new_path)
        if current_path == move_path:
            return JsonResponse({
                "result":
                1,
                "message":
                "Cannot move '" +
                current_path.replace(self.user_storage.path(""), "") +
                "' into itself"
            })  # Error
        try:
            shutil.move(current_path, move_path)
            if ShareUrl.objects.filter(owner=self.current_user,
                                       path=old_path).exists():
                share_url = get_object_or_404(ShareUrl,
                                              owner=self.current_user,
                                              path=old_path)
                # Remove leading slash
                new_url_path = move_path.replace(
                    self.user_storage.path(""), "") + "/" + os.path.basename(
                        os.path.normpath(old_path))
                while new_url_path.startswith("/"):
                    new_url_path = new_url_path[1:]
                share_url.path = new_url_path
                share_url.save()
        except shutil.Error as ex:
            # Strip user directory location and return error
            return JsonResponse({
                "result":
                1,
                "message":
                str(ex).replace(self.user_storage.path(""), "")
            })  # Some sort of error
        return JsonResponse({"result": 0, "message": "success"})  # Success

    def rename(self, file_path, new_name):
        try:
            rename_path = os.path.join(
                self.user_storage.path(self.user_directory), file_path)
        except Exception:
            # File not found
            return False
        new_name_path = os.path.join(self.user_storage.path(rename_path),
                                     "../")
        new_name_path = os.path.join(self.user_storage.path(new_name_path),
                                     new_name)
        if os.path.exists(new_name_path):
            return False
        else:
            os.rename(rename_path, new_name_path)
            # Update links if necessary
            if ShareUrl.objects.filter(owner=self.current_user,
                                       path=file_path).exists():
                share_url = get_object_or_404(ShareUrl,
                                              owner=self.current_user,
                                              path=file_path)
                # Remove leading slash
                new_url_path = new_name_path.replace(
                    self.user_storage.path(""), "")
                while new_url_path.startswith("/"):
                    new_url_path = new_url_path[1:]
                share_url.path = new_url_path
                share_url.save()
            return True

    def file_search(self, search_item):
        file_list = []
        # Recursively search through user directory
        for filename in Path(self.user_storage.path("")).glob('**/' +
                                                              search_item):
            file_list.append(
                str(filename).replace(
                    self.user_storage.path(""),
                    ""))  # Remove absolute path to home directory
        return JsonResponse({
            'result': 0,
            'files': file_list,
            'search_query': search_item
        })