Ejemplo n.º 1
0
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('image_data',
                                type=str,
                                help="Image Base, base64 format")
            parser.add_argument('name', type=str, help="Site Title")
            parser.add_argument('url', type=str, help="URL")

            args = parser.parse_args()

            image_data = args.get('image_data', None)
            name = args.get('name', None)
            url = args.get('url', False)

            parent = None

            _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)

            filename = secure_filename("{0}.png".format(rand_str(10)))
            to_path = os.path.join(_dir, filename)

            pattern = re.compile(r'^data:image/png;base64,', re.IGNORECASE)
            image_data = re.sub(pattern, '', image_data)

            img_bytes = base64.b64decode(image_data)
            with open(to_path, 'bw+') as f:
                f.write(img_bytes)

            print(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,
                               siteName=name,
                               url=url)

        except Exception as e:
            abort(500,
                  message=
                  "There was an error while processing your request --> {0}".
                  format(e))
Ejemplo n.º 2
0
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('image_data',
                                type=str,
                                help="Image Base, base64 format")
            parser.add_argument('site_name', type=str, help="Site Title")
            parser.add_argument('url', type=str, help="URL")

            args = parser.parse_args()

            image_data = args.get('image_data', None)
            site_name = args.get('site_name', None)
            url = args.get('url', False)

            parent = None

            _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)

            filename = secure_filename("{0}.png".format(rand_str(10)))
            to_path = os.path.join(_dir, filename)

            pattern = re.compile(r'^data:image/png;base64,', re.IGNORECASE)
            image_data = re.sub(pattern, '', image_data)

            img_bytes = base64.b64decode(image_data)
            with open(to_path, 'bw+') as f:
                f.write(img_bytes)

            print(to_path)

            fileuri = os.path.join("{0}/".format(_path), filename)
            filesize = os.path.getsize(to_path)

            new_file = File.create(name=filename,
                                   uri=fileuri,
                                   size=filesize,
                                   parent=parent,
                                   creator=g.user_id,
                                   site_name=site_name,
                                   url=url)

            try:
                token = jwt.encode({'id': g.user_id},
                                   current_app.config['SECRET_KEY'],
                                   algorithm='HS256')
            except JWTError:
                raise ValidationError(
                    "There was a problem while trying to create a JWT token.")

            first_task_id = ocr_pipeline(token, new_file['id'])
            logger.warn('first_task_id')

            return new_file

        except Exception as e:
            abort(500,
                  message=
                  "There was an error while processing your request --> {0}".
                  format(e))
Ejemplo n.º 3
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))