Exemplo n.º 1
0
def test_destroy():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id
    f.destroy()
Exemplo n.º 2
0
def upload(file_path):
    print 'uploading file %s' % file_path
    img_name = path.split(file_path)[1]
    img_file = open(file_path)
    up_file = File(img_name, img_file)
    img_url = up_file.save().url
    return img_url
Exemplo n.º 3
0
def test_destroy():
    r = requests.get('http://www.lenna.org/full/len_std.jpg')
    b = buffer(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id
    f.destroy()
Exemplo n.º 4
0
def test_destroy():  # type: () -> None
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = io.BytesIO(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id
    f.destroy()
def upload(file_path):
    if os.path.exists(file_path):
        img_name = os.path.split(file_path)[1]
        img_file = open(file_path)
        up_file = File(img_name, img_file)
        img_url = up_file.save().url
        print(" url: %s" % img_url)
        return img_url
Exemplo n.º 6
0
def test_save_with_specified_key_but_without_master_key():  # type: () -> None
    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    f.key = "abc"
    try:
        f.save()
    except LeanCloudError as e:
        if e.code == 1 and e.error.startswith("Unsupported file key"):
            pass
Exemplo n.º 7
0
def test_thumbnail():
    r = requests.get('http://www.lenna.org/full/len_std.jpg')
    f = File('Lenna2.jpg', r.content)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Exemplo n.º 8
0
def test_thumbnail():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Exemplo n.º 9
0
Arquivo: lzmd.py Projeto: chinaq/blog
def upload(file_path):
    print file_path
    img_name = path.split(file_path)[1]
    with open(file_path, 'rb') as f:
        img_file = read_full_file(f)  # Qiang
        up_file = File(img_name, buffer(img_file))
        up_file.save()
        img_url = up_file.url
        return img_url
Exemplo n.º 10
0
def test_thumbnail():
    r = requests.get('http://www.lenna.org/full/len_std.jpg')
    b = buffer(r.content)
    f = File('lenna.jpg', b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Exemplo n.º 11
0
def upload(file_path):
    cmp_file_path = dirpath + file_path
    if os.path.exists(cmp_file_path):
        img_name = os.path.split(cmp_file_path)[1]
        img_file = open(cmp_file_path)
        up_file = File(img_name, img_file)
        img_url = up_file.save().url
        # print(" url: %s" % img_url)
        return img_url
Exemplo n.º 12
0
def test_thumbnail():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Exemplo n.º 13
0
def test_thumbnail():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith("?imageView/2/w/100/h/100/q/100/format/png")
Exemplo n.º 14
0
def test_save_external():  # type: () -> None
    file_name = "lenna.jpg"
    file_url = "http://i1.wp.com/leancloud.cn/images/static/default-avatar.png"
    f = File.create_with_url(file_name, file_url)
    f.save()
    assert f.id
    file_on_cloud = File.create_without_data(f.id)
    file_on_cloud.fetch()
    assert file_on_cloud.name == file_name
    assert file_on_cloud.url == file_url
Exemplo n.º 15
0
def get_img(url):
    try:
        fn = url.split('/')[-1]
        c = urllib2.urlopen(url).read()
        f = StringIO(c)
        lc_file = File(fn, f)
        lc_file.save()
        r = lc_file.url
    except Exception, e:
        print '---------', e
        r = None
Exemplo n.º 16
0
def get_img(url):
    try:
        fn = url.split('/')[-1]
        c = urllib2.urlopen(url).read()
        f = StringIO(c)
        lc_file = File(fn, f)
        lc_file.save()
        r = lc_file.url
    except Exception, e:
        print '---------', e
        r = None
Exemplo n.º 17
0
def test_save():  # type: () -> None
    user = leancloud.User()
    user.login('user1_name', 'password')

    f = File('Blah.txt', open('tests/sample_text.txt', 'rb'))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == 'Blah.txt'
    assert f.mime_type == 'text/plain'
    assert not f.url.endswith('.')
Exemplo n.º 18
0
def test_save():  # type: () -> None
    user = leancloud.User()
    user.login('user1_name', 'password')

    f = File('Blah.txt', open('tests/sample_text.txt', 'rb'))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == 'Blah.txt'
    assert f.mime_type == 'text/plain'
    assert not f.url.endswith('.')
Exemplo n.º 19
0
 def upload_file(self, file_abspath):
     filename = os.path.basename(file_abspath)    # filename have suffix
     with open(file_abspath, 'r') as f:
         upload_file = File(filename, f)
         upload_file.save()
         print 'uploaded', file_abspath
         img_file = self._class()
         img_file.set('File', upload_file)
         img_file.set('filename', filename)
         tag_list = LeanCloudApi.get_tag_list(filename)
         img_file.set('tag_list', tag_list)
         img_file.save()
         self.add_img_info(img_file.id)    # save img_info after save
Exemplo n.º 20
0
 def upload_file(self, file_abspath):
     filename = os.path.basename(file_abspath)  # filename have suffix
     with open(file_abspath, 'r') as f:
         upload_file = File(filename, f)
         upload_file.save()
         print 'uploaded', file_abspath
         img_file = self._class()
         img_file.set('File', upload_file)
         img_file.set('filename', filename)
         tag_list = LeanCloudApi.get_tag_list(filename)
         img_file.set('tag_list', tag_list)
         img_file.save()
         self.add_img_info(img_file.id)  # save img_info after save
Exemplo n.º 21
0
def test_save_with_specified_key():  # type: () -> None
    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    user_specified_key = "abc"
    f.key = user_specified_key
    f.save()

    assert f.id
    assert f.name == "Blah.txt"
    assert f.mime_type == "text/plain"
    path = urlparse(f.url).path
    if path.startswith("/avos-cloud-"):  # old school aws s3 file url
        assert path.split("/")[2] == user_specified_key
    else:
        assert path == "/" + user_specified_key
Exemplo n.º 22
0
def test_file_callback():  # type: () -> None
    d = {}

    def noop(token, *args, **kwargs):
        d['token'] = token

    f = File('xxx', io.BytesIO(b'xxx'))
    f._save_to_s3 = noop
    f._save_to_qiniu = noop
    f._save_to_qcloud = noop
    f.save()
    f._save_callback(d['token'], False)
Exemplo n.º 23
0
def test_file_callback():  # type: () -> None
    d = {}

    def noop(token, *args, **kwargs):
        d["token"] = token

    f = File("xxx", io.BytesIO(b"xxx"))
    f._save_to_s3 = noop
    f._save_to_qiniu = noop
    f._save_to_qcloud = noop
    f.save()
    f._save_callback(d["token"], False)
Exemplo n.º 24
0
def test_basic():
    s = BytesIO(b'blah blah blah')
    if PY2:
        import cStringIO
        s1 = cStringIO.StringIO()
        s1.write('blah blah blah')
    else:
        s1 = s
    f1 = File('Blah', s, 'text/plain')
    f2 = File('Blah', s1)
    f3 = File('Blah', open('tests/test_file.txt', 'rb'))
    for f in (f1, f2, f3):
        assert f.name == 'Blah'
        assert f._metadata['size'] == 14
        assert f.size == 14
        assert f._type == 'text/plain'
Exemplo n.º 25
0
def test_create_with_url():  # type: () -> None
    f = File.create_with_url(
        "xxx",
        u"http://i1.wp.com/leancloud.cn/images/static/default-avatar.png",
        meta_data={},
    )
    assert f.url == "http://i1.wp.com/leancloud.cn/images/static/default-avatar.png"
Exemplo n.º 26
0
def test_thumbnail_size_erorr():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Exemplo n.º 27
0
def test_thumbnail_size_erorr():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Exemplo n.º 28
0
    def on_message(self, message):
        logging.info(message)
        msg = json.loads(message['data'])
        musics = msg.get('musics')

        # download
        for music in musics:
            song_name = music.get('song_name')
            song_id = music.get('song_id')
            singer_name = music.get('singer_name')
            singer_id = music.get('singer_id')
            audition_list = music.get('audition_list')
            album_id = music.get('album_id')
            album_name = music.get('album_name')

            audition = audition_list[-1]
            url = audition.get('url')
            duration = audition.get('duration')
            suffix = audition.get('suffix')
            bit_rate = audition.get('bitRate')
            type_description = audition.get('typeDescription')
            size = audition.get('size')
            pic_url = music.get('mv_list')[0].get('pic_url') if music.get(
                'mv_list') else ''

            if not Query(Music).equal_to('song_id', song_id).find():
                filename = u'{}-{}.mp3'.format(song_name, singer_name)
                f = File(filename,
                         StringIO.StringIO(urllib2.urlopen(url).read()))
                f.save()

                picture = None
                if pic_url:
                    picture_name = u'{}-{}.jpg'.format(song_name, singer_name)
                    picture = File(
                        picture_name,
                        StringIO.StringIO(urllib2.urlopen(pic_url).read()))
                    picture.save()

                m = Music()
                m.file = f
                m.duration = duration
                m.suffix = suffix
                m.bit_rate = bit_rate
                m.type_description = type_description
                m.size = size
                m.song_name = song_name
                m.song_id = song_id
                m.singer_name = singer_name
                m.singer_id = singer_id
                m.album_id = album_id
                m.album_name = album_name
                if picture:
                    m.picture = picture
                m.save()
Exemplo n.º 29
0
 def post(self, username):
     profile_fields = ['realName', 'gender', 'school', 'grade', 'major', 'about']
     userProfile = Query(UserProfile).equal_to("user", username).first()
     try:
         for key in profile_fields:
             userProfile.set(key, self.get_argument(key, ""))
             if 'file' in self.request.files:
                 file_dict_list = self.request.files['file']
                 for file_dict in file_dict_list:
                     data = file_dict["body"]
                     avatar = File("avatar", buffer(data))
                     avatar.save()
             userProfile.set("avatar", avatar.get_thumbnail_url(width='200', height='200'))
             userProfile.save()
             self.redirect('/user/'+username)
             print self.request
     except LeanCloudError, e:
         pass
Exemplo n.º 30
0
def test_thumbnail_size_erorr():
    r = requests.get('http://www.lenna.org/full/len_std.jpg')
    f = File('Lenna2.jpg', r.content)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Exemplo n.º 31
0
def test_file_field():  # type: () -> None
    user = User()
    user.login('user1_name', 'password')
    user.set('xxxxx', File('xxx.txt', io.BytesIO(b'qqqqq')))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get('xxxxx'), File)
    assert saved_user.get('xxxxx').name == 'xxx.txt'
Exemplo n.º 32
0
def test_file_field():  # type: () -> None
    user = User()
    user.login("user1_name", "password")
    user.set("xxxxx", File("xxx.txt", io.BytesIO(b"qqqqq")))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get("xxxxx"), File)
    assert saved_user.get("xxxxx").name == "xxx.txt"
Exemplo n.º 33
0
def test_file_field():
    user = User()
    user.login('user1_name', 'password')
    user.set('xxxxx', File('xxx.txt', buffer('qqqqq')))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get('xxxxx'), File)
    assert saved_user.get('xxxxx').name == 'xxx.txt'
Exemplo n.º 34
0
def test_file_callback():  # type: () -> None
    d = {}

    def noop(token, *args, **kwargs):
        d['token'] = token

    f = File('xxx', io.BytesIO(b'xxx'))
    f._save_to_s3 = noop
    f._save_to_qiniu = noop
    f._save_to_qcloud = noop
    f.save()
    f._save_callback(d['token'], False)
Exemplo n.º 35
0
def test_thumbnail_size_erorr():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Exemplo n.º 36
0
def test_basic():  # type: () -> None
    s = BytesIO(b'blah blah blah')
    if PY2:
        import cStringIO  # type: ignore
        s1 = cStringIO.StringIO()
        s1.write('blah blah blah')
    else:
        s1 = s
    f = File('Blah', s, mime_type='text/plain')
    assert f.name == 'Blah'
    assert f._metadata['size'] == 14
    assert f.size == 14
Exemplo n.º 37
0
def test_save():  # type: () -> None
    user = leancloud.User()
    name = 'user1_name'
    passwd = 'password'
    try:
        user.login(name, passwd)
    except LeanCloudError as e:
        if e.code == 211:
            user.set_username(name)
            user.set_password(passwd)
            user.sign_up()
            user.login(name, passwd)

    f = File('Blah.txt', open('tests/sample_text.txt', 'rb'))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == 'Blah.txt'
    assert f.mime_type == 'text/plain'
    assert not f.url.endswith('.')
Exemplo n.º 38
0
def test_save():  # type: () -> None
    user = leancloud.User()
    name = "user1_name"
    passwd = "password"
    try:
        user.login(name, passwd)
    except LeanCloudError as e:
        if e.code == 211:
            user.set_username(name)
            user.set_password(passwd)
            user.sign_up()
            user.login(name, passwd)

    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == "Blah.txt"
    assert f.mime_type == "text/plain"
    assert not f.url.endswith(".")
Exemplo n.º 39
0
def test_destroy():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id
    f.destroy()
Exemplo n.º 40
0
def test_destroy():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id
    f.destroy()
Exemplo n.º 41
0
def upload():
    if not session.get('session_token'):
        return redirect(url_for('accounts_bp.login'))

    app_id = session.get('app_id', None)
    developer = Developer()
    developer.session_token = session.get('session_token')
    username = developer.username()
    app_list = developer.get_app_list()

    if request.method == "POST":
        cert = request.files.get('cert')
        key = request.files.get('key')
        app_id = request.form.get('appId')
        passphrase = request.form.get('passphrase')
        if app_id not in map(lambda x: x['app_id'], app_list):
            flash("This appId owned by others", "alert")
            return redirect(url_for("settings.upload"))

        cert_pem = File(app_id + '_cert.pem', StringIO(cert.read()))
        key_pem = File(app_id + '_key.pem', StringIO(key.read()))
        cert_pem.metadata.update(owner=username)
        key_pem.metadata.update(owner=username)
        cert_url = cert_pem.save().url
        key_url = key_pem.save().url

        app_query = Query(Application)
        app_query.equal_to('objectId', app_id)
        app = app_query.first()

        if app:
            app.set('cert_url', cert_url)
            app.set('key_url', key_url)
            app.set('passphrase', passphrase)
            app.save()
        flash("Certificate has added done!")
        return redirect(url_for("settings.upload"))
    return render_template('settings/upload.html', username=username,
                           app_id=app_id, app_list=app_list)
Exemplo n.º 42
0
def test_fetch():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.metadata['foo'] = 'bar'
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()
    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.url == f.url
    assert fetched.size == f.size
    assert fetched.url == f.url
    f.destroy()
Exemplo n.º 43
0
def upload_image(fname):        
    HEIGHT, WIDTH = 384, 240
    #c = request.files['file'].read()
    with open(fname, 'rb') as f:
        c = f.read()
    file_orig = StringIO(c)
    im = Image.open(file_orig)
    h, w = im.size
    if h > HEIGHT or w > WIDTH:
        im.thumbnail((HEIGHT, WIDTH), Image.ANTIALIAS)
        file = StringIO()
        h, w = im.size
        if h==w:
            im = im.rotate(90 * 3)
        try:
            im.save(file, 'JPEG')
        except:
            #for .gif
            file = file_orig
    else:
        file = file_orig
    lc_file = File('pi', file_orig)
    lc_file.save()
    return lc_file.url 
Exemplo n.º 44
0
def test_fetch():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.metadata["foo"] = "bar"
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()

    normalized_f_url = f.url.split("/")[-1]
    normalized_fetched_url = f.url.split("/")[-1]

    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.size == f.size
    assert fetched.url == f.url or normalized_fetched_url == normalized_f_url
    f.destroy()
Exemplo n.º 45
0
def test_fetch():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer(r.content)
    f = File('Lenna2.jpg', b)
    f.metadata['foo'] = 'bar'
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()
    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.url == f.url
    assert fetched.size == f.size
    assert fetched.url == f.url
    f.destroy()
Exemplo n.º 46
0
 def upload_file_by_url(self, filename, url, tag_list=None):
     """tag_list is tag of string list"""
     data = LeanCloudApi.fetch_data(url)
     if not data:
         return
     data = data.content
     f = File(filename, StringIO(data))
     img_file = self._class()
     img_file.set('File', f)
     img_file.set('filename', filename)
     if tag_list:
         img_file.set('tag_list', tag_list)
     try:
         img_file.save()
         print filename, '----uploaded----'
         self.add_img_info(img_file.id)  # save img_info after save
     except:
         'save failed'
         return
Exemplo n.º 47
0
def test_fetch():  # type: () -> None
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = io.BytesIO(r.content)
    f = File('Lenna2.jpg', b)
    f.metadata['foo'] = 'bar'
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()

    normalized_f_url = f.url.split('/')[-1]
    normalized_fetched_url = f.url.split('/')[-1]

    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.size == f.size
    assert fetched.url == f.url or normalized_fetched_url == normalized_f_url
    f.destroy()
Exemplo n.º 48
0
def test_create_without_data():
    f = File.create_without_data(123)
    assert f.id == 123
Exemplo n.º 49
0
def test_acl():  # type: () -> None
    acl_ = ACL()
    f = File('Blah', io.BytesIO(b'xxx'))
    assert_raises(TypeError, f.set_acl, 'a')
    f.set_acl(acl_)
    assert f.get_acl() == acl_
Exemplo n.º 50
0
def test_thumbnail_url_erorr():
    f = File.create_with_url('xx', False)
    f.get_thumbnail_url(100, 100)
Exemplo n.º 51
0
def test_save_external():
    f = File.create_with_url('lenna.jpg', 'http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    f.save()
    assert f.id
Exemplo n.º 52
0
def test_thumbnail_url_erorr():  # type: () -> None
    f = File.create_with_url('xx', '')
    f.get_thumbnail_url(100, 100)
Exemplo n.º 53
0
def test_save():
    f = File('blah', buffer('xxx'))
    f.save()
    assert f.id
Exemplo n.º 54
0
def test_save():
    f = File('Blah', 'xxx')
    f.save()
    assert f.id
Exemplo n.º 55
0
def test_create_with_url():
    f = File.create_with_url('xxx', 'http://www.lenna.org/full/len_std.jpg', meta_data={})
    assert f.url == 'http://www.lenna.org/full/len_std.jpg'
Exemplo n.º 56
0
def test_acl():
    acl = ACL()
    f = File('Blah', buffer('xxx'))
    assert_raises(TypeError, f.set_acl, 'a')
    f.set_acl(acl)
    assert f.get_acl() == acl
Exemplo n.º 57
0
def test_save():
    f = File('Blah', buffer('xxx'))
    f.save()
    assert f.id
Exemplo n.º 58
0
def test_create_without_data():  # type: () -> None
    f = File.create_without_data('a123')
    assert f.id == 'a123'
Exemplo n.º 59
0
def test_create_with_url():
    f = File.create_with_url('xxx', 'http://i1.wp.com/leancloud.cn/images/static/default-avatar.png', meta_data={})
    assert f.url == 'http://i1.wp.com/leancloud.cn/images/static/default-avatar.png'
Exemplo n.º 60
0
def test_save_external():
    f = File.create_with_url('lenna.jpg', 'http://www.lenna.org/full/len_std.jpg')
    f.save()
    assert f.id