예제 #1
0
def upload_file(file, user_id):
    """
    Function adds file to Files table and saves it in users directory
    :param file: object of request.files
    :param user_id: id of User uploading file
    :return: File and DataSet id's
    """

    upload_dir = user_dir(user_id)
    path = save_path(upload_dir, file.filename, user_id)
    if os.path.exists(path):
        file_id = rewrite_file(file, path)
        dataset_id = Dataset.query.filter(
            Dataset.file_id == file_id).first().id
        return "Updated", file_id, dataset_id

    # filesystem manipulation
    create_dir(upload_dir)  # creates directory recursively if not exist
    file.save(path)  # saving file to filesystem
    shape = serialize(path)
    file_attr = attributes(path)  # getting attributes from file
    file_attr['name'] = file.filename
    file_attr['rows'] = shape[0]
    # db manipulation
    new_file = File(path=os.path.basename(path),
                    attributes=file_attr)  # adding uploaded File to DB
    db.session.add(new_file)
    db.session.flush()
    new_dataset = Dataset(user_id=user_id,
                          file_id=new_file.id)  # adding empty DataSet to DB
    db.session.add(new_dataset)
    db.session.commit()

    return 'Uploaded', new_file.id, new_dataset.id
예제 #2
0
    def all_files(self):
        from app.models.files import File

        aws_files = self._list_files()
        files = []
        for file in aws_files:
            file = File().load_by_attribute("path", file["Key"])
            if file is not None and file.can_current_user_view:
                files.append(file)

        return files
예제 #3
0
 def add_file(cls, file_name, attributes):
     """
     Adds file with given fields to database
     :param file_name: string
     :param attributes: json
     :return: file object
     """
     file = File(file_name, attributes)
     DB.session.add(file)
     DB.session.commit()
     return file
예제 #4
0
 def batch_add_file(self, files):
     try:
         db.session.bulk_save_objects(
             File(uuid=str(self.uuid()),
                  file_name=file['file_name'],
                  file_size=file['file_size'],
                  creator_id=file['user_id'],
                  status=CREATE_STATUS.value) for file in files)
         db.session.commit()
     except IntegrityError:
         raise FileDuplicated
     except Exception:
         raise FileBaseError
예제 #5
0
 def add_file(self, file_json, user_id):
     try:
         file = file_schema.load(file_json,
                                 partial=("file_name", "file_size"))
         file = File(uuid=str(self.uuid()),
                     file_name=file.file_name,
                     file_size=file.file_size,
                     creator_id=user_id,
                     status=CREATE_STATUS.value)
         db.session.add(file)
         db.session.commit()
         return file_schema.dump(file)
     except IntegrityError:
         raise FileDuplicated
     except Exception:
         raise FileBaseError
예제 #6
0
def search(text=None):
    """
    Search the files for a set of words and return any matches.
    If no search terms are given then return all files.
    """

    if text:
        # Split the text into individual words
        individual_words = wordsplit_pattern.findall(text)

        files = File.search(individual_words)

    else:
        files = File.query.all()

    return jsonify(files=files)
예제 #7
0
    def all_files(self):
        from app.models.files import File

        file_names = [
            f for f in os.listdir(self.folder)
            if os.path.isfile(os.path.join(self.folder, f))
        ]

        files = []

        for file in file_names:
            file = File().load_first_by_attribute("path", file)
            if file is not None and file.can_current_user_view:
                files.append(file)

        return files
예제 #8
0
def publish():
    """
    From a multipart/form-data request add or update a file in the system
    """
    name = request.form.get('name')
    uploaded_file = request.files['file']
    mime = uploaded_file.content_type

    file = File.find_or_create(name, mime)
    file.file_name = uploaded_file.filename
    file.process_upload(uploaded_file)

    # Allow the endpoint to be called directly from the browser
    if 'browser' in request.args:
        return redirect('/upload?success=true')

    return jsonify(file=file)
예제 #9
0
 def before_download(self, id):
     self.file = File.load(id)
     self.validate_show(self.file)
예제 #10
0
 def before_delete(self, id):
     self.file = File.load(id)
     self.validate_delete(self.file)
예제 #11
0
 def before_show(self, hash_value):
     self.file = File.load_by_attribute("hash", hash_value)
     self.validate_show(self.file)