Esempio n. 1
0
 def __init__(self,
              bucket=BUCKET_NAME,
              bucket_url=BUCKET_URL,
              enable_link_token=ENABLE_LINK_TOKEN,
              link_token=LINK_TOKEN,
              link_expiration=LINK_EXPIRATION,
              operator_name=OPERATOR_NAME,
              operator_password=OPERATOR_PASSWORD):
     self.bucket = bucket
     self.operator_name = operator_name
     self.operator_password = operator_password
     self.bucket_url = bucket_url
     self.enable_link_token = enable_link_token
     self.link_token = link_token
     self.link_expiration = link_expiration
     if not bucket or not operator_name or not operator_password:
         raise ImproperlyConfigured(
             'bucket , opeartor_name, operator_password is requied')
     self.upyun_client = UpYun(self.bucket, self.operator_name,
                               self.operator_password)
Esempio n. 2
0
    def up_to_upyun(collection, data):
        img_data = data.get("body")
        img_name = data.get("filename").encode("utf-8")

        bucket = conf["upyun_bucket"]
        admin = conf["upyun_admin"]
        password = conf["upyun_password"]

        u = UpYun(bucket, admin, password)
        u.setApiDomain("v0.api.upyun.com")
        # TODO u.setContentMD5(md5file(data))

        # save file
        year = datetime.datetime.now().strftime("%Y")
        month = datetime.datetime.now().strftime("%m")
        day = datetime.datetime.now().strftime("%d")
        target = "/%s/%s/%s/%s/%s" % (collection, year, month, day, img_name)
        a = u.writeFile(str(target), img_data, True)
        url = conf["upyun_url"] + str(target)
        return url
Esempio n. 3
0
 def POST(self):   
     
     name=web.input().get("name")
     _tags=web.input().get("tags")
     source=web.input().get("source")                 
     if _tags:
          tags=[x for x in  _tags.split(",")]
     else:
          tags=[]
     user=User.objects(id=Auth.uid()).first()     
     creatorID=user.ID     
     intro=web.input().get("intro")
     picurl=web.input().get("picurl",None)
     price=web.input().get("price")
     link=web.input().get("source")
     store=get_store("store")
     price=web.input().get("price")
     buyinfo=Buy_info(link=link,store=store,price=price)
     #check if none 
     pic=urllib2.urlopen(picurl).read()
     ID=Node.objects.count()+1
     open(str(ID)+".jpg","wb").write(pic)
     u = UpYun(BUCKETNAME,USER,PASSWORD)
     data = open(str(ID)+".jpg",'rb')
     u.setContentMD5(md5file(data))
     a = u.writeFile('/o'+str(ID)+'.jpg',data)
     if not a:
       return "get picture erro"
     #cut_item_picture(STATIC_FILE,ID,str(ID)+".jpg")  
     node=Node(creatorID=creatorID,ID=ID,name=name,des=intro,picurl=picurl,tags=tags,buy_info=[buyinfo])
     node.picinfo=101    #a out sidelink
     node.save()
     share=Share_items(ID=Share_items.objects.count()+1,creatorID=user.ID,itemID=ID,flag=1,content="")
     share.save()
     #加入到shareitem中
     #todo count更新
     tweet=Tweet(ID=Tweet.objects.count()+1,AutherID=user.ID,SubjectID=ID,flag=1,content=str(ID))
     tweet.save()
     return web.seeother("/item/"+str(ID)+"/")
Esempio n. 4
0
 def POST(self):
          picurl=web.input().get("check")
          name=web.input().get("name")
          _tags=web.input().get("tags")
          source=web.input().get("source")                 
          if _tags:
            tags=[x for x in  _tags.split(",")]
          else:
            tags=[]
          user=User.objects(id=Auth.uid()).first()     
          creatorID=user.ID     
          intro=web.input().get("intro")
          price=web.input().get("price")
          link=web.input().get("source")
          store=web.input().get("store")
          #store=get_store(link).decode("utf-8")
          price=web.input().get("price")
          buyinfo=Buy_info(link=link,store=store,price=price)
          pic=urllib2.urlopen(picurl).read()
          ID=Node.objects.count()+1
          open(str(ID)+".jpg","wb").write(pic)
          u = UpYun(BUCKETNAME,USER,PASSWORD)
          data = open(str(ID)+".jpg",'rb')
          u.setContentMD5(md5file(data))
          a = u.writeFile('/o'+str(ID)+'.jpg',data)
          if not a:
              return "get picture erro"
          #cut_item_picture(STATIC_FILE,ID,str(ID)+".jpg")  
          node=Node(creatorID=creatorID,ID=ID,name=name,des=intro,picurl=picurl,tags=tags,buy_info=[buyinfo])
          node.picinfo=101    #a out sidelink
          node.save()
          share=Share_items(ID=Share_items.objects.count()+1,creatorID=user.ID,itemID=ID,flag=1,content="")
          share.save()
          content=u'''<h1>发布成功</h1><p>你可以<a href="/item/%s/">去看看</a>或者
          <a href="%s">回到刚才的逛的</a>'''%(str(ID),source)
          return render.info(content=content)
          return web.seeother("/item/"+str(ID)+"/")
Esempio n. 5
0
 def __init__(self, bucket=BUCKET_NAME,
              bucket_url=BUCKET_URL,
              enable_link_token=ENABLE_LINK_TOKEN,
              link_token=LINK_TOKEN,
              link_expiration=LINK_EXPIRATION,
              operator_name=OPERATOR_NAME,
              operator_password=OPERATOR_PASSWORD):
     self.bucket = bucket
     self.operator_name = operator_name
     self.operator_password = operator_password
     self.bucket_url = bucket_url
     self.enable_link_token = enable_link_token
     self.link_token = link_token
     self.link_expiration = link_expiration
     if not bucket or not operator_name or not operator_password:
         raise ImproperlyConfigured('bucket , opeartor_name, operator_password is requied')
     self.upyun_client = UpYun(self.bucket, self.operator_name, self.operator_password)
Esempio n. 6
0
class UpYunStorage(Storage, ThumbnailSupportMixin):
    """UpYun Storage class for Django pluggable storage system."""
    def __init__(self,
                 bucket=BUCKET_NAME,
                 bucket_url=BUCKET_URL,
                 enable_link_token=ENABLE_LINK_TOKEN,
                 link_token=LINK_TOKEN,
                 link_expiration=LINK_EXPIRATION,
                 operator_name=OPERATOR_NAME,
                 operator_password=OPERATOR_PASSWORD):
        self.bucket = bucket
        self.operator_name = operator_name
        self.operator_password = operator_password
        self.bucket_url = bucket_url
        self.enable_link_token = enable_link_token
        self.link_token = link_token
        self.link_expiration = link_expiration
        if not bucket or not operator_name or not operator_password:
            raise ImproperlyConfigured(
                'bucket , opeartor_name, operator_password is requied')
        self.upyun_client = UpYun(self.bucket, self.operator_name,
                                  self.operator_password)

    def _open(self, name, mode='rb'):
        if 'w' not in mode and not self.exists(name):
            raise IOError('File does not exist: %s' % name)
        return UpYunStorageFile(name, mode, self)

    def _save(self, name, content):
        print 'try to save ....', name
        self._put_file(name, content.read())
        content.close()
        return name

    def _put_file(self, name, content):
        name = self._clean_name(name)
        self.upyun_client.writeFile(name, content, auto=True)

    def _read(self, name):
        name = self._clean_name(name)
        content = self.upyun_client.readFile(name)
        if content is None:
            raise UpYunStorageException('fail to read file content')
        return content

    def _clean_name(self, name):
        if type(name) is unicode:
            return name.encode('utf-8')
        else:
            return name

    #def get_valid_name(self, name):
    #    print 'get valid name', name
    #    return name

    def listdir(self, path):
        return [d.filename for d in self.upyun_client.readDir(path)]

    def delete(self, name):
        try:
            return self.upyun_client.delete(name)
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

    def exists(self, name):
        try:
            return self.upyun_client.getFileInfo(name) is not None
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

    def size(self, name):
        try:
            fileinfo = self.upyun_client.getFileInfo(name)
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

        if not fileinfo:
            raise UpYunStorageException('file %s not exists' % name)
        return fileinfo['size']

    def url(self, name):
        if not self.bucket_url:
            raise ImproperlyConfigured('bucket_url is required for url method')
        if self.enable_link_token and not self.link_token:
            raise ImproperlyConfigured(
                'link token is required , if enalbe_link_token is on')
        url = urlparse.urljoin(self.bucket_url, name)
        if self.enable_link_token:
            etime = int(time.time() + self.link_expiration)
            if name[0] != '/':
                name = '/' + name
            sign = hashlib.md5(
                '%s&%s&%s' %
                (self.link_token, etime, name)).hexdigest()[12:20] + str(etime)
            url = url + '?_upt=' + sign
        return url
Esempio n. 7
0
class UpYunStorage(Storage, ThumbnailSupportMixin):
    """UpYun Storage class for Django pluggable storage system."""


    def __init__(self, bucket=BUCKET_NAME,
                 bucket_url=BUCKET_URL,
                 enable_link_token=ENABLE_LINK_TOKEN,
                 link_token=LINK_TOKEN,
                 link_expiration=LINK_EXPIRATION,
                 operator_name=OPERATOR_NAME,
                 operator_password=OPERATOR_PASSWORD):
        self.bucket = bucket
        self.operator_name = operator_name
        self.operator_password = operator_password
        self.bucket_url = bucket_url
        self.enable_link_token = enable_link_token
        self.link_token = link_token
        self.link_expiration = link_expiration
        if not bucket or not operator_name or not operator_password:
            raise ImproperlyConfigured('bucket , opeartor_name, operator_password is requied')
        self.upyun_client = UpYun(self.bucket, self.operator_name, self.operator_password)

    def _open(self, name, mode='rb'):
        if 'w' not in mode and not self.exists(name):
            raise IOError('File does not exist: %s' % name)
        return UpYunStorageFile(name, mode, self)

    def _save(self, name, content):
        print 'try to save ....', name
        self._put_file(name, content.read())
        content.close()
        return name

    def _put_file(self, name, content):
        name = self._clean_name(name)
        self.upyun_client.writeFile(name, content, auto=True)

    def _read(self, name):
        name = self._clean_name(name)
        content = self.upyun_client.readFile(name)
        if content is None:
            raise UpYunStorageException('fail to read file content')
        return content

    def _clean_name(self, name):
        if type(name) is unicode:
            return name.encode('utf-8')
        else:
            return name

    #def get_valid_name(self, name):
    #    print 'get valid name', name
    #    return name

    def listdir(self, path):
        return [d.filename for d in self.upyun_client.readDir(path)]

    def delete(self, name):
        try:
            return self.upyun_client.delete(name)
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

    def exists(self, name):
        try:
            return self.upyun_client.getFileInfo(name) is not None
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

    def size(self, name):
        try:
            fileinfo = self.upyun_client.getFileInfo(name)
        except UpYunException:
            raise UpYunStorageException(unicode(UpYunException))

        if not fileinfo:
            raise UpYunStorageException('file %s not exists' %name)
        return fileinfo['size']

    def url(self, name):
        if not self.bucket_url:
            raise ImproperlyConfigured('bucket_url is required for url method')
        if self.enable_link_token and not self.link_token:
            raise ImproperlyConfigured('link token is required , if enalbe_link_token is on')
        url = urlparse.urljoin(self.bucket_url, name)
        if self.enable_link_token:
            etime = int(time.time() + self.link_expiration)
            if name[0] != '/':
                name = '/' + name
            sign = hashlib.md5('%s&%s&%s' %(self.link_token,etime,name)).hexdigest()[12:20] + str(etime)
            url = url + '?_upt=' + sign
        return url