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 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 #4
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 #5
0
 def resource_hash(data_bin: bytes):
     res_hash = sha256_bin_digest(data_bin)
     return res_hash
 def component_hash(word: str):
     word_bts = word.encode(encoding='utf-8')
     return sha256_bin_digest(word_bts)
Beispiel #7
0
 def component_hash_by_term(term):
     bts = term.encode(encoding='utf-8')
     return sha256_bin_digest(bts)
Beispiel #8
0
 def component_hash(specifier: str, prfx: str):
     spec_word = '%s::%s' % (specifier, prfx)
     bts = spec_word.encode(encoding='utf-8')
     return sha256_bin_digest(bts)
Beispiel #9
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