Beispiel #1
0
def add_new_file(id, file_path):
    download_file = OfflineDownload.query.filter(
        OfflineDownload.id == id).first()
    userid = download_file.userid
    filename = download_file.filename
    uploaddate = download_file.time
    # path = download_file.path
    filesize = os.path.getsize(file_path)
    # find the file and calculate the md5.
    with open(file_path, 'rb') as f:
        m = md5()
        r = f.read(MAX_READ_SIZE)
        m.update(r)
        while len(r) > 0:
            r = f.read(MAX_READ_SIZE)
            m.update(r)
        md5_str = m.hexdigest()
    filetype = FileType.query.filter(
        FileType.filetype == check_file_type(filename)).first()
    if not filetype:
        raise TypeError('can not specify filetype for {}'.format(filename))
    filetype = filetype.id
    file = File(userid=userid,
                filetype=filetype,
                filename=filename,
                filesize=filesize,
                uploaddate=uploaddate,
                path=download_file.path,
                md5=md5_str)
    db.session.add(file)
    # move the file to destination dir
    new_path = FILE_PATH + os.sep + md5_str
    shutil.move(file_path, new_path)
def upload_request():
    try:
        user_id = current_user.id
    except AttributeError:
        abort(404)
    file = request.files['file']
    size = int(request.form['filesize'])
    path = request.form['path']
    md5 = request.form['md5']
    upload_date = datetime.now()
    file_name = file.filename
    file_type_name = check_file_type(file_name)
    filetype = model.FileType.query.filter(
        model.FileType.filetype == file_type_name).first()
    if not filetype:
        return jsonify(api_result("failure", "can't identify the file type."))
    filetype = filetype.id

    max_sequence_number = math.ceil(size / SPLIT_SIZE)
    uc = UploadCache.create(id=UploadCache.generate_id(),
                            max_sequence_number=max_sequence_number,
                            size=size,
                            path=path,
                            md5=md5,
                            upload_date=upload_date,
                            file_name=file_name,
                            filetype=filetype,
                            user_id=user_id)

    return jsonify(
        api_result(SUCCESS_RESULT, task_id=uc.id, split_size=SPLIT_SIZE))
 def test_document_type(self):
     self.assertEqual(check_file_type("C:\\myfile.pdf"), self.DOCUMENT_TYPE)
 def test_audio_type(self):
     self.assertEqual(check_file_type("/etc/myfile.mp4.fLaC"),
                      self.AUDIO_TYPE)
 def test_video_type(self):
     self.assertEqual(check_file_type("/foo/bar/myfile.mP4"),
                      self.VIDEO_TYPE)
 def test_image_type(self):
     self.assertEqual(check_file_type("myfile.jpg"), self.IMAGE_TYPE)
     self.assertEqual(check_file_type("/favoico.ICO"), self.IMAGE_TYPE)
 def test_other_type(self):
     self.assertEqual(check_file_type("myfile"), self.OTHER_TYPE)
     self.assertEqual(check_file_type("myfile.123"), self.OTHER_TYPE)