go-pd icon indicating copy to clipboard operation
go-pd copied to clipboard

A free easy to use pixeldrain.com go client pkg and CLI upload tool.

Version GitHub GitHub code size in bytes GitHub go.mod Go version GitHub top language

Go-PD - A pixeldrain.com client pkg and CLI tool

Go-PD

A free pixeldrain.com client written in go. We use the super power from imroc/req (v3.43.x) to build a robust and fast pixeldrain client and cobra for our CLI tool.

Go-PD

Content:

  • Using the CLI tool
    • CLI Tool: Install
    • CLI Tool: Upload a file
    • CLI Tool: Download a file
  • Using the client pkg
    • Why?
    • Import pkg
    • Example 1 - the easiest way to upload an anonymous file
    • Example 2 - advanced way to upload a file to user account
  • ToDo's
  • Covered methods
  • License

Using the CLI tool

CLI Tool: Install

Follow the link to download the correct binary for your system. View Releases. It's available for Linux, ARM and Windows. Download the correct archive, extract it and use the binary.

CLI Tool: Upload a file

Go to the folder where you download the binary file and run the following command in a CLI.

Simple upload:

 ./go-pd upload my-cat.jpg
 
 Output:
 https://pixeldrain.com/u/aaaaaaaa

Upload to your account (-verbose): The verbose option enable also the progress in % and the env check message.

 ./go-pd upload -k <your-api-key> -v my-cat.jpg my-cat2.jpg
 
 Output:
 Using API Key from environment variable: PIXELDRAIN_API_KEY
 "my-cat.jpg" uploaded 100.00%
 Successful! Anonymous upload: false | ID: xBxxxxxx | URL: https://pixeldrain.com/u/xBxxxxxx
 "my-cat2.jpg" uploaded 100.00%
 Successful! Anonymous upload: false | ID: xAxxxxxx | URL: https://pixeldrain.com/u/xAxxxxxx

CLI Tool: Download a file

Go to the folder where you download the binary file and run the following command in a CLI.

Simple download:

 ./go-pd download https://pixeldrain.com/u/YqiUjXXX
 
 Output:
 /the/path/to/your/file

Download multiple files to a specific path (-verbose):

 ./go-pd download -k <your-api-key> -p /home/pixeldrain/pictures/ YqiUjXXX YqiUjX02 YqiUjX03
 
 Output:
 Successful! Download complete: filename01.jpg | ID: xBxxxxxx | Stored to: /home/pixeldrain/pictures/filename01.jpg
 Successful! Download complete: filename02.jpg | ID: xBxxxxxx | Stored to: /home/pixeldrain/pictures/filename02.jpg
 Successful! Download complete: filename03.jpg | ID: xBxxxxxx | Stored to: /home/pixeldrain/pictures/filename03.jpg

Using the client pkg

Why the package?

Because we want a simple, fast, robust and tested go package to upload to pixeldrain.com.

Import the pkg

 go get github.com/ManuelReschke/go-pd/pkg/pd

Example 1 - the easiest way to upload an anonymous file

package main

import (
	"fmt"
	"time"

	"github.com/ManuelReschke/go-pd/pkg/pd"
)

func main() {
	req := &pd.RequestUpload{
		PathToFile: "testdata/cat.jpg",
		Anonymous:  true,
	}

	c := pd.New(nil, nil)
	rsp, err := c.UploadPOST(req)
	if err != nil {
		fmt.Println(err)
	}

	// print the full URL
	fmt.Println(rsp.GetFileURL())

        // example ID = xFNz76Vp
        // example URL = https://pixeldrain.com/u/xFNz76Vp
}

Example 2 - advanced way - upload a file to user account with progress callback

package main

import (
	"fmt"
	"time"

	"github.com/ManuelReschke/go-pd/pkg/pd"
    "github.com/imroc/req/v3"
)

func main() {
	req := &pd.RequestUpload{
		PathToFile: "testdata/cat.jpg",
		FileName:   "test_post_cat.jpg",
		Anonymous:  false,
		Auth: pd.Auth{
			APIKey: "you-api-key-from-pixeldrain-account",
		},
	}

	// set specific request options
	opt := &pd.ClientOptions{
		Debug:             false,
		ProxyURL:          "example.socks5.proxy",
		EnableCookies:     true,
		EnableInsecureTLS: true,
		Timeout:           1 * time.Hour,
	}

	c := pd.New(opt, nil)
	
	// enable progress
    c.SetUploadCallback(func(info req.UploadInfo) {
      if info.FileSize > 0 {
        fmt.Printf("%q uploaded %.2f%%\n", info.FileName, float64(info.UploadedSize)/float64(info.FileSize)*100.0)
      } else {
        fmt.Printf("%q uploaded 0%% (file size is zero)\n", info.FileName)
      }
    })
	
	rsp, err := c.UploadPOST(req)
	if err != nil {
		fmt.Println(err)
	}

	// print the full URL
	fmt.Println(rsp.GetFileURL())

        // example ID = xFNz76Vp
        // example URL = https://pixeldrain.com/u/xFNz76Vp
}

ToDo's:

  • [x] implement simple upload method over POST /file
  • [x] implement simple upload over PUT /file/{filename}
  • [x] write unit tests
  • [x] write integration tests
  • [x] add API-KEY auth to requests
  • [x] implement all other API methods
    • [x] implement GET - /file/{id}
    • [x] implement GET - /file/{id}/info
    • [x] implement GET - /file/{id}/thumbnail?width=x&height=x
    • [x] implement DELETE - /file/{id}
    • [x] implement POST - /list
    • [X] implement GET - /list/{id}
    • [x] implement GET - /user
    • [x] implement GET - /user/files
    • [x] implement GET - /user/lists
  • [x] create CLI tool for uploading to pixeldrain.com
  • [x] update imroc/req to the latest version
  • [ ] refactor the hole shit and use nice to have patterns (like Option Pattern)

PixelDrain methods covered by this package

File Methods

PixelDrain Call Package Func
[x] POST - /file UploadPOST(r *RequestUpload) (*ResponseUpload, error)
[x] PUT - /file/{name} UploadPUT(r *RequestUpload) (*ResponseUpload, error)
[x] GET - /file/{id} Download(r *RequestDownload) (*ResponseDownload, error)
[x] GET - /file/{id}/info GetFileInfo(r *RequestFileInfo) (*ResponseFileInfo, error)
[x] GET - /file/{id}/thumbnail?width=x&height=x DownloadThumbnail(r *RequestThumbnail) (*ResponseThumbnail, error)
[x] DELETE - /file/{id} Delete(r *RequestDelete) (*ResponseDelete, error)

List Methods

PixelDrain Call Package Func
[x] POST - /list CreateList(r *RequestCreateList) (*ResponseCreateList, error)
[x] GET - /list/{id} GetList(r *RequestGetList) (*ResponseGetList, error)

User Methods

PixelDrain Call Package Func
[x] GET - /user GetUser(r *RequestGetUser) (*ResponseGetUser, error)
[x] POST - /user/files GetUserFiles(r *RequestGetUserFiles) (*ResponseGetUserFiles, error)
[x] GET - /user/lists GetUserLists(r *RequestGetUserLists) (*ResponseGetUserLists, error)

Package CLI commands

Unit Tests - Run pkg unit tests

Run unit tests against a local emulated server.

make test

Integration Tests - Run pkg integration tests

Run real integration tests against the real pixeldrain.com website.

make test-integration

Test Coverage - create test coverage report

Create a coverage report c.out and a coverage.html to view the results in web browser

make coverage

Thanks to

Special thanks to Visual Studio Code and to Jetbrains for this amazing IDE and supporting the open source community.

License

This software is released under the MIT License, see LICENSE.