Esempio n. 1
0
    def func(*args, **kwargs):
        file_id = kwargs.get('file_id')

        payload = _decode_jwt()
        g.user_id = payload['id']

        file = File.find(file_id, True)
        if not file or file['creator'] != g.user_id:
            abort(404, message="The file you are trying to access was not found")
        g.file = file
        return f(*args, **kwargs)
Esempio n. 2
0
    def put(self, file_id):
        try:
            update_fields = {}
            parser = reqparse.RequestParser()

            parser.add_argument('name',
                                type=str,
                                help="New name for the file/folder")
            parser.add_argument('parent_id',
                                type=str,
                                help="New parent folder for the file/folder")

            args = parser.parse_args()

            name = args.get('name', None)
            parent_id = args.get('parent_id', None)

            if name is not None:
                update_fields['name'] = name

            if parent_id is not None and g.file['parent_id'] != parent_id:
                if parent_id != '0':
                    folder_access = Folder.filter({
                        'id': parent_id,
                        'creator': g.user_id
                    })
                    if not folder_access:
                        abort(
                            404,
                            message=
                            "You don't have access to the folder you're trying to move this object to"
                        )

                if g.file['is_folder']:
                    update_fields['tag'] = g.file[
                        'id'] if parent_id == '0' else "{0}#{1}".format(
                            folder_access['tag'], folder['last_index'])
                    Folder.move(g.file, folder_access)
                else:
                    File.move(g.file, folder_access)

                update_fields['parent_id'] = parent_id

            if g.file['is_folder']:
                Folder.update(file_id, update_fields)
            else:
                File.update(file_id, update_fields)

            return File.find(file_id)
        except Exception as e:
            abort(500,
                  message="There was an while processing your request --> {0}".
                  format(e))
Esempio n. 3
0
 def get(self, file_id):
     try:
         file_data = File.find(file_id)
         #print(file_data)
         parts = os.path.split(file_data['uri'])
         file_path = os.path.join(BASE_DIR, parts[0])
         #print(file_path)
         return send_from_directory(directory=file_path,
                                    filename=parts[1],
                                    as_attachment=True)
     except Exception as e:
         abort(500,
               message="There was an while processing your request --> {0}".
               format(e))
Esempio n. 4
0
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument(
                'name',
                type=str,
                help="This should be the folder name if creating a folder")
            parser.add_argument('parent_id',
                                type=str,
                                help='This should be the parent folder id')
            parser.add_argument(
                'is_folder',
                type=bool,
                help=
                "This indicates whether you are trying to create a folder or not"
            )

            args = parser.parse_args()

            name = args.get('name', None)
            parent_id = args.get('parent_id', None)
            is_folder = args.get('is_folder', False)

            parent = None

            # Are we adding this to a parent folder?
            if parent_id is not None:
                parent = File.find(parent_id)
                if parent is None:
                    raise Exception("This folder does not exist")
                if not parent['is_folder']:
                    raise Exception("Select a valid folder to upload to")
            # Are we creating a folder?

            if is_folder:
                if name is None:
                    raise Exception(
                        "You need to specify a name for this folder")

                return Folder.create(name=name,
                                     parent=parent,
                                     is_folder=is_folder,
                                     creator=g.user_id)

            else:
                files = request.files['file']

                if files:
                    if not is_allowed(files.filename):
                        raise Exception("File not allowed: {0}".format(
                            files.filename))

                    _path = os.path.join(current_app.config['UPLOAD_FOLDER'],
                                         g.user_id)
                    _dir = os.path.join(BASE_DIR, "{0}/".format(_path))

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

                    filetitle, fileext = os.path.splitext(files.filename)
                    filename = secure_filename("{0}_{1}{2}".format(
                        filetitle, rand_str(10), fileext))
                    to_path = os.path.join(_dir, filename)
                    files.save(to_path)
                    fileuri = os.path.join("{0}/".format(_path), filename)
                    filesize = os.path.getsize(to_path)

                    return File.create(name=filename,
                                       uri=fileuri,
                                       size=filesize,
                                       parent=parent,
                                       creator=g.user_id)
                raise Exception(
                    "You did not supply a valid file in your request")

        except Exception as e:
            abort(500,
                  message=
                  "There was an error while processing your request --> {0}".
                  format(e))