Esempio n. 1
0
class UpYunStore(object):
    APP_NAME = None
    USERNAME = None
    PASSWORD = None
    TMP_PATH = None

    def __init__(self, uri):
        assert uri.startswith('http://')
        self.upyun = UpYun(self.APP_NAME, self.USERNAME, self.PASSWORD)
        self.prefix = '/' + uri.split('/')[-1]

    def stat_image(self, key, info):
        image_info = self.upyun.getinfo(self.prefix + '/' + key)
        last_modified = int(image_info['date'])
        checksum = image_info['size']
        return {'last_modified': last_modified, 'checksum': checksum}

    def persist_image(self, key, image, buf, info):
        tmp_path = os.path.join(self.TMP_PATH, 'tmp.jpg')
        image.save(tmp_path)
        data = open(tmp_path, 'rb')
        result = self.upyun.put(self.prefix + '/' + key, data, True)
        if not result:
            log.info("Image: Upload image to Upyun Failed! %s" %
                     (self.prefix + key))
Esempio n. 2
0
def app(environ, start_response):
    path = environ['PATH_INFO']
    data = ''
    if path == "/list.html":
        query = environ['QUERY_STRING']
        m = re.match(r'^dir=/([^/]*)(.*)', query)
        bucket, fpath = m.group(1), m.group(2)

        up = UpYun(bucket = bucket, username = config.username, 
                password = config.password
            )

        info = up.getinfo(key = fpath)
        if info['file-type'] == 'folder':
            status = '200 OK'
            data = listdir(up, bucket, fpath)
            start_response(status, [
                ("Content-Type", "text/html"),
                ("Content-Length", str(len(data)))
            ])
        else:
            status = '302 Temporarily Moved'
            start_response(status, [
                ("Content-Type", "text/plain"),
                ("Content-Length", str(len(data)))
                (
                    "Location", "http://%s.b0.upaiyun.com%s" % 
                    (bucket, fpath)
                ),
            ])
    elif path == "/upload.html":
        m = re.match(r'uri=(.*)&dir=/([^/]*)(.*)', environ['QUERY_STRING'])
        uri, bucket, fpath = m.group(1), m.group(2), m.group(3)
        blob = ''
        resp = requests.get(uri, stream=True, timeout=5)
        for chunk in resp.iter_content(8192):
            if not chunk:
                break
            blob += chunk
        if blob != '':
            up = UpYun(bucket = bucket, username = config.username, 
                    password = config.password
                )
            up.put(fpath, blob)
        data = '%s Upload to /%s%s .. OK' % (uri, bucket, fpath)

    else:
        status = '200 OK'
        data += '<h1>usage</h1>\n'
        data += '<p>/list.html?dir=</bucket/path/to/file></p>\n'
        data += '<p>/post.html?uri=<uri>&&dir=</bucket/path/to/file></p>\n'
        start_response(status, [
            ("Content-Type", "text/html"),
            ("Content-Length", str(len(data)))
        ])

    return iter([data])
Esempio n. 3
0
class UpYunStore(object):
    APP_NAME = None
    USERNAME = None
    PASSWORD = None
    TMP_PATH = None

    def __init__(self, uri):
        assert uri.startswith('http://')
        self.upyun = UpYun(self.APP_NAME, self.USERNAME, self.PASSWORD)
        self.prefix = '/' + uri.split('/')[-1]

    def stat_image(self, key, info):
        image_info = self.upyun.getinfo(self.prefix + '/' + key)
        last_modified = int(image_info['date'])
        checksum = image_info['size']
        return {'last_modified': last_modified, 'checksum': checksum}

    def persist_image(self, key, image, buf, info):
        tmp_path = os.path.join(self.TMP_PATH, 'tmp.jpg')
        image.save(tmp_path)
        data = open(tmp_path, 'rb')
        result = self.upyun.put(self.prefix + '/' + key, data, True)
        if not result:
            log.info("Image: Upload image to Upyun Failed! %s" % (self.prefix + key))