RGAlex

Test

Sep 8th, 2022
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react'
  2. import { Web3Storage } from 'web3.storage'
  3.  
  4. function Storage() {
  5.   const [image, setImage] = useState()
  6.   const client = new Web3Storage({
  7.     token: process.env.NEXT_PUBLIC_API_TOKEN,
  8.   })
  9.   const obj = { hello: 'world' }
  10.  
  11.   async function storeFiles() {
  12.     const files = [new File(v, 'Test image')]
  13.     console.log(files)
  14.     try {
  15.       const cid = await client.put(files)
  16.       console.log('stored files with cid:', cid)
  17.       return cid
  18.     } catch (err) {
  19.       console.log(err)
  20.     }
  21.   }
  22.  
  23.   async function retrieve() {
  24.     const res = await client.get(
  25.       'bafybeic4rawbxffcelx6ryeskeinhhafypzvppg6bzhu5mt2zgj5o626lu'
  26.     )
  27.     console.log(`Got a response! [${res.status}] ${res.statusText}`)
  28.     if (!res.ok) {
  29.       throw new Error(
  30.         `failed to get ${'bafybeic4rawbxffcelx6ryeskeinhhafypzvppg6bzhu5mt2zgj5o626lu'}`
  31.       )
  32.     } // unpack File objects from the response
  33.     const files = await res.files()
  34.     for (const file of files) {
  35.       console.log(
  36.         `${file.cid} -- ${file.name} -- ${file.lastModified} -- ${file.size}`
  37.       )
  38.     }
  39.   }
  40.  
  41.   // async function checkStatus() {
  42.   //   const status = await client.status(
  43.   //     'bafybeihtfx2jznesd5iifu32y2igy77wrsr4szey3ou7csggghiv2f675m'
  44.   //   )
  45.   //   console.log(status)
  46.   //   if (status) {
  47.   //     // your code to do something fun with the status info here
  48.   //   }
  49.   // }
  50.  
  51.   // // replace with your own CID to see info about your uploads!
  52.   // checkStatus('bafybeihtfx2jznesd5iifu32y2igy77wrsr4szey3ou7csggghiv2f675m')
  53.   return (
  54.     <div className="space-x-4 flex">
  55.       <button
  56.         onClick={storeFiles}
  57.         className="flex w-60 rounded-2xl px-2 py-1 font-mono font-bold text-gray-400 text-xl bg-white/5"
  58.       >
  59.         <a className="flex justify-center rounded-2xl hover:border border-stone-500 w-full">
  60.           StoreFiles{' '}
  61.           <svg
  62.             xmlns="http://www.w3.org/2000/svg"
  63.             fill="none"
  64.             viewBox="0 0 24 24"
  65.             strokeWidth={1.5}
  66.             stroke="currentColor"
  67.             className="h-6 w-6 ml-2 text-green-500"
  68.           >
  69.             <path
  70.               strokeLinecap="round"
  71.               strokeLinejoin="round"
  72.               d="M12 16.5V9.75m0 0l3 3m-3-3l-3 3M6.75 19.5a4.5 4.5 0 01-1.41-8.775 5.25 5.25 0 0110.233-2.33 3 3 0 013.758 3.848A3.752 3.752 0 0118 19.5H6.75z"
  73.             />
  74.           </svg>
  75.         </a>{' '}
  76.       </button>
  77.       <button
  78.         onClick={retrieve}
  79.         className="flex w-60 rounded-2xl px-2 py-1 font-mono font-bold text-gray-400 text-xl bg-white/5"
  80.       >
  81.         <a className="flex justify-center rounded-2xl hover:border border-stone-500 w-full">
  82.           Retrieve{' '}
  83.           <svg
  84.             xmlns="http://www.w3.org/2000/svg"
  85.             fill="none"
  86.             viewBox="0 0 24 24"
  87.             strokeWidth={1.5}
  88.             stroke="currentColor"
  89.             className="h-6 w-6 ml-2 text-green-500"
  90.           >
  91.             <path
  92.               strokeLinecap="round"
  93.               strokeLinejoin="round"
  94.               d="M12 16.5V9.75m0 0l3 3m-3-3l-3 3M6.75 19.5a4.5 4.5 0 01-1.41-8.775 5.25 5.25 0 0110.233-2.33 3 3 0 013.758 3.848A3.752 3.752 0 0118 19.5H6.75z"
  95.             />
  96.           </svg>
  97.         </a>{' '}
  98.       </button>
  99.       <input
  100.         multiple
  101.         type="file"
  102.         onChange={(e) => {
  103.           setV(e.target.files)
  104.         }}
  105.         files={image}
  106.       />
  107.       <img src="phantom.png" className="h-8 w-8" />
  108.     </div>
  109.   )
  110. }
  111.  
  112. export default Storage
  113.  
Advertisement
Add Comment
Please, Sign In to add comment