Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.4 kB
1
Indexable
Never
async function test() {
  const fs = require('fs');
  const crypto = require('crypto');
  const { default: axios } = require("axios");
  const accessToken = 'token';
  const filePath = './favicon.png';
  
function getRandomFileName(originalPath) {
    const extension = originalPath.split('.').pop();
    const randomName = crypto.randomBytes(8).toString('hex');
    return `${randomName}.${extension}`;
}

async function uploadAndGetLink() {
   console.log("Загружаем файл")
  const upload = await axios.post('https://content.dropboxapi.com/2/files/upload', fs.readFileSync(filePath), {
      headers: {
          'Authorization': `Bearer ${accessToken}`,
          'Dropbox-API-Arg': JSON.stringify({ path: dropboxPath }),
          'Content-Type': 'application/octet-stream'
      }
  });
  
  
    if (upload.status !== 200) {
      console.error('Ошибка загрузки файла');
      return;
  }


  console.log("Достаем ссылку")
  const link = await axios.post('https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings', {
      path: dropboxPath
  }, {
      headers: {
          'Authorization': `Bearer ${accessToken}`,
          'Content-Type': 'application/json'
      }
  });

  if (link.status === 200) {
      console.log('Ссылка:', link.data.url);
  } else {
      console.error('Error getting shared link.');
  }
}
    
}

test()