Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baidu-netdisk

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baidu-netdisk

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Baidu Netdisk

Gem Version CI codecov Maintainability

A Ruby client to upload files to Baidu NetDisk (PCS). It'll auto-split big files and upload slices parallelly.

Installation

Install with bundler:

bundle add baidu-netdisk

Or install with ruby gem

gem install baidu-netdisk

Configuration

BaiduNetDisk.config do |c|
  # Required configs
  c.app_id     = 'your_app_id'
  c.app_key    = 'your_app_key'
  c.secret_key = 'your_secret_key'

  # Max threads for uploading file slices, default to 1
  c.max_uploading_threads = 4

  # The following two are optional;
  # Fill them in if you want to explicitly indicate uploading to someone else's storage space
  c.access_token  = 'your_access_token'
  c.refresh_token = 'your_refresh_token'
end

Usage

This client is designed for the two following scenarios.

Scenario 1: Using Baidu NetDisk as a backup file storage

You always save files to your net disk storage. You don't need a callback to receive auth codes or tokens from Baidu; Instead, you can just get them from your Ruby console.

require 'baidu-netdisk'

# It opens a browser to ask for access to Baidu OAuth;
# you will get your auth code after you log in to your Baidu NetDisk
BaiduNetDisk::Auth.get_auth_code

# Pass the code as the argument your received from the previous step
BaiduNetDisk::Auth.get_token(auth_code)

uploader = BaiduNetDisk::Uploader.new(source_path, target_path)
uploader.execute

#  => {"category"=>6, "ctime"=>1676019860, "from_type"=>1, "fs_id"=>121127634951625, "isdir"=>0, "md5"=>"79835de6btc0b3482f51b49088c8ccfb", "mtime"=>1676019860, "path"=>"<target_path>", "server_filename"=>"<file_name>", "size"=>76267, "errno"=>0, "name"=>"<target_path>"} 

Scenario 2: Upload files to your clients' Baidu NetDisk

You should have a web server and need to implement your webhook to receive a callback from Baidu NetDisk after auth.

Say your callback URL is https://www.example.com/webhook, you should have the:

BaiduNetDisk::Auth.get_auth_code('https://www.example.com/webhook')


# Auth code is fetched in your callbacks
BaiduNetDisk::Auth.get_token(auth_code, 'https://www.example.com/webhook')

uploader = BaiduNetDisk::Uploader.new(source_path, target_path,
  { access_token: 'client.access_token', refresh_token: 'client.refresh_token' })

uploader.execute

It provides a hook after token refreshed so you can save tokens after refreshing.

BaiduNetDisk.config do |c|
  c.after_token_refreshed = -> (access_token, refresh_token) {
    user.update access_token: access_token, refresh_token: refresh_token
  }
end

Development

Copy the env file

It reads ENV from .env for testing.

cp .env.example .env

Run tests

bundle install
bundle exec rake

Todo

  1. Improve MAINTAINABILITY

  2. To use other HTTP clients instead of RestClient:

  3. RestClient can't catch response body when response is 400 BadRequest.

  4. Currently, we implemented multi-threaded uploading by hands. Consider to use typhoeus with its native hydra to run HTTP requests in parallel.

FAQs

Package last updated on 03 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc