Beispiel #1
0
    def POST(self, ufile):
        if self.enable_cors:
            self.set_cors_headers()
        ct = None
        fext = None
        try:
            ct = str(ufile.content_type)
            fext = ct.split('/')[1]
            if not ct:
                self.cherry.response.status = 408
                return b'{"error":"can\'t determine content-type"}'
        except:
            self.cherry.response.status = 408
            return b'{"error":"can\'t determine content-type"}'
        size = 0
        uf = io.BytesIO()
        while True:
            data = ufile.file.read(8192)
            if not data:
                break
            uf.write(data)
            size += len(data)

        uf.seek(0)
        uf_bts = uf.read()
        hk = cdx.guid_bin_to_hex(cdx.sha256_bin_digest(uf_bts))
        st_hk = hk.decode()
        f_name = '%s.%s' % (st_hk, fext)
        f_name_meta = '%s.json' % st_hk
        upload_file = os.path.join(self.store_dir, f_name)
        upload_meta_file = os.path.join(self.store_dir, f_name_meta)

        with open(upload_file, 'wb') as u_f:
            uf.seek(0)
            u_f.write(uf.read())
        u_f.close()
        # resize for thumbnails

        try:
            self.thumbnail.resize(fn=upload_file, max_w=400, max_h=400)
        except Exception as e:
            print('POST: WARNING THUMBNAIL FAILED', e)

        with open(upload_meta_file, 'wb') as u_m_f:
            u_m_f.write(
                json.dumps({
                    'fext': fext,
                    'hkey': st_hk,
                    "Content-Type": ct
                },
                           ensure_ascii=False).encode())
        u_m_f.close()

        self.on_post(st_hk)

        self.cherry.response.status = 201
        return hk.decode()
Beispiel #2
0
 def __call__(self, *args, **kwargs):
     key, val = args
     key_st = json.dumps(key, ensure_ascii=False)
     val_st = json.dumps(val, ensure_ascii=False)
     key_val = key_st + val_st
     key_val_bts = key_val.encode()
     hash_bin = sha256_bin_digest(key_val_bts)
     hash_bin_ascii = guid_bin_to_hex(hash_bin)
     return hash_bin_ascii
Beispiel #3
0
 def GET(self):
     # todo
     c = self.dhtf.dht.storage.pubkeys.cursor.execute(
         'SELECT bkey from ertref;')
     guid_list = [guid_bin_to_hex(k[0]).decode() for k in c.fetchall()]
     # print(guid_list)
     js = json.dumps(guid_list)
     js_b = js.encode()
     return js_b
Beispiel #4
0
 def GET(self, owner_guid=None):
     if owner_guid:
         ll = self.get_per_guid(owner_guid)
         d_js = json.dumps(ll, ensure_ascii=False)
         d_sj_bin = d_js.encode()
         return d_sj_bin
     else:
         c = self.dhf.dht.storage.pubkeys.cursor.execute('SELECT bkey from ertref;')
         guid_list = [guid_bin_to_hex(k[0]).decode() for k in c.fetchall()]
         ll = list()
         for guid in guid_list:
             ll.append({guid: self.get_per_guid(guid)})
         d_js = json.dumps(ll, ensure_ascii=False)
         d_sj_bin = d_js.encode()
         return d_sj_bin
Beispiel #5
0
    def POST(self, ufile):
        cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
        ct = None
        try:
            ct = str(ufile.content_type)
            fext = ct.split('/')[1]
            if not ct:
                self.cherry.response.status = 408
                return b'{"error":"can\'t determine content-type"}'
        except:
            self.cherry.response.status = 408
            return b'{"error":"can\'t determine content-type"}'
        size = 0
        uf = io.BytesIO()
        while True:
            data = ufile.file.read(8192)
            if not data:
                break
            uf.write(data)
            size += len(data)

        uf.seek(0)
        uf_bts = uf.read()
        hk = guid_bin_to_hex(sha256_bin_digest(uf_bts))
        st_hk = hk.decode()
        f_name = '%s.%s' % (st_hk, fext)
        f_name_meta = '%s.json' % st_hk
        upload_file = os.path.join(self.store_dir, f_name)
        upload_meta_file = os.path.join(self.store_dir, f_name_meta)

        with open(upload_file, 'wb') as u_f:
            uf.seek(0)
            u_f.write(uf.read())
        u_f.close()

        with open(upload_meta_file, 'wb') as u_m_f:
            u_m_f.write(
                json.dumps({
                    'fext': fext,
                    'hkey': st_hk,
                    "Content-Type": ct
                },
                           ensure_ascii=False).encode())
        u_m_f.close()
        self.on_post(st_hk)
        self.cherry.response.status = 201
        return hk.decode()
Beispiel #6
0
 def POST(self):
     try:
         body = self.cherrypy.request.body.read()
         if body:
             content_type = self.cherrypy.request.headers[
                 'Content-Type'].encode('utf8')
             content_encoding = 'identity'.encode('utf-8')
             pk_bin = self.api.create(body,
                                      content_type=content_type,
                                      content_encoding=content_encoding)
             pk_hex = guid_bin_to_hex(pk_bin)
             self.cherrypy.response.status = 201
             return pk_hex
         self.cherrypy.response.status = 400
         return b'null'
     except:
         traceback.print_exc()
         self.cherrypy.response.status = 400
         return b'null'
Beispiel #7
0
    def __call__(self, pk_hash_bin, rep_maj=0):
        bin_rs, content_type, content_encoding = self.api.query(
            pk_hash=pk_hash_bin)
        print('B', bin_rs)
        if bin_rs:
            if content_type != 'application/json':
                print("WARN content type not supported", content_type)
            try:
                d = json.loads(bin_rs.decode('utf-8'))
                d['ownerReputation'] = rep_maj
                return d

            except:
                print("WARN failed object decoding",
                      guid_bin_to_hex(pk_hash_bin))
                return None
        else:
            print("WARN bin res is None", bin_rs)
            return None
Beispiel #8
0
    def create(
        self,
        json_data: bytes,
        owner_hash: bytes,
        resource_signature: bytes,
        content_type=None,
        content_encoding=None,
    ):
        if not content_type:
            content_type = self.content_type
        if not content_encoding:
            content_encoding = self.content_encoding

        pk_hash = self.data_store.create_resource(owner_hash,
                                                  resource_signature,
                                                  content_type,
                                                  content_encoding, json_data)

        res = SignedBinResource()
        res.orig_res_hash = sha256_bin_digest(json_data)
        res.content_type = content_type
        res.content_encoding = content_encoding
        res.owner_bin = owner_hash
        res.data_bin = json_data
        res.sig_bin = resource_signature
        pk_hash_hex = guid_bin_to_hex(pk_hash).decode()

        if self.cdn_client:
            cdn_pk_hash = self.cdn_client.create(res)
            if cdn_pk_hash == pk_hash_hex:
                # all good
                print('RESOURCE CREATE ON CDN PEER')
                print('RES:', cdn_pk_hash)
                pass
            else:
                print('Warning cdn hash not equal to local pk hash')
                print('CDN:', cdn_pk_hash)
                print('LOC:', pk_hash_hex)
        return pk_hash
Beispiel #9
0
    def GET(self, owner_guid=None, deleted=None):

        if deleted and owner_guid:
            dl = instance_dl(self.dhf, owner_guid, self.delete_collection_name)
            ll = list(dl.iter_hk(inverted=False))
            d_js = json.dumps(ll, ensure_ascii=False)
            d_sj_bin = d_js.encode()
            return d_sj_bin

        if owner_guid:
            ll = self.get_per_guid(owner_guid)
            d_js = json.dumps(ll, ensure_ascii=False)
            d_sj_bin = d_js.encode()
            return d_sj_bin
        else:
            c = self.dhf.dht.storage.pubkeys.cursor.execute('SELECT bkey from ertref;')
            guid_list = [guid_bin_to_hex(k[0]).decode() for k in c.fetchall()]
            ll = list()
            for guid in guid_list:
                ll.append({guid: self.get_per_guid(guid)})
            d_js = json.dumps(ll, ensure_ascii=False)
            d_sj_bin = d_js.encode()
            return d_sj_bin
Beispiel #10
0
 def hashid_list(self, owner_hash):
     ll = self.data_store.list_by_owner(owner_hash)
     if ll:
         return [guid_bin_to_hex(k[0]).decode() for k in ll]
Beispiel #11
0
    def POST(self):
        try:
            body = self.cherrypy.request.body.read()
            if body:
                pk_bin = self.api.create(body)
                pk_hex = guid_bin_to_hex(pk_bin)

                # indexing

                js = body.decode('utf-8')
                d = json.loads(js)
                title = d.get('title')
                description = d.get('description')
                category = d.get('categoryName')
                experience_level = d.get('experienceName')
                job_type = d.get('jobTypeName')

                experience_level = None
                # experience_level = '_'.join(experience_level.lower().split(' '))
                category = '_'.join(category.lower().split(' '))
                # job_type = '_'.join(job_type.lower().split(' '))

                budget = None
                price_st = d.get('price')
                price = None
                try:
                    price = int(price_st)
                except:
                    print('ERR with price to int')
                if price:
                    budget = calculate_price_range(price)

                print('CAT LEV', category, experience_level)

                price = d.get('price')

                if title:
                    self.text_api.idx_engine.index_bag_of_spec_text(
                        container_hash=pk_bin,
                        specifier='title',
                        text_data=title)
                if description:
                    self.text_api.idx_engine.index_bag_of_spec_text(
                        container_hash=pk_bin,
                        specifier='description',
                        text_data=description)

                # if experience_level:
                #     self.text_api.idx_engine.index_bag_of_spec_text(
                #         container_hash=pk_bin, specifier='experience_level', text_data=experience_level)

                if category:
                    self.text_api.idx_engine.index_bag_of_spec_text(
                        container_hash=pk_bin,
                        specifier='category',
                        text_data=category)

                # if job_type:
                #     self.text_api.idx_engine.index_bag_of_spec_text(container_hash=pk_bin,
                #                                                     specifier='job_type',
                #                                                     text_data=job_type)

                if budget:
                    self.text_api.idx_engine.index_bag_of_spec_text(
                        container_hash=pk_bin,
                        specifier='budget',
                        text_data=budget)

                # self.text_api.idx_engine.index_bag_of_spec_text(
                #     container_hash=pk_bin, specifier='job_type', text_data=job_type)

                self.cherrypy.response.status = 201
                return pk_hex
            self.cherrypy.response.status = 404
            return b'null'
        except:
            traceback.print_exc()
            self.cherrypy.response.status = 400
            return b'null'
 def hashid_list_all(self):
     ll = self.data_store.list_all()
     if ll:
         return [guid_bin_to_hex(k[0]).decode() for k in ll]
Beispiel #13
0
 def __call__(self, pk_hash_bin, rep_maj=None):
     return guid_bin_to_hex(pk_hash_bin).decode('utf-8')
Beispiel #14
0
def bytes_to_hkey_and_io(bts: bytes) -> (str, BytesIO):
    bio = BytesIO(bts)
    bio.seek(0)
    hk = guid_bin_to_hex(sha256_bin_digest(bts))
    return hk, bio