pan

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateMD5 added in v0.1.1

func CalculateMD5(filePath string) (string, error)

CalculateMD5 calculates the MD5 hash of a given file

func CalculateSliceMD5 added in v0.1.1

func CalculateSliceMD5(filePath string, sliceSize int64) ([]string, error)

CalculateSliceMD5 calculates MD5 hashes for fixed-size slices of a file

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes converts bytes to a human-readable format (e.g., KB, MB, GB)

func FormatDiskInfo

func FormatDiskInfo(info *DiskInfoResponse) string

FormatDiskInfo formats the disk information in a human-readable way

func FormatFileInfo

func FormatFileInfo(fileInfo *FileInfo) string

FormatFileInfo formats the file information in a human-readable way

func FormatTime

func FormatTime(unixTime int64) string

FormatTime converts Unix timestamp to readable time format

func GetCopyErrorMessage added in v0.1.1

func GetCopyErrorMessage(errno int) string

GetCopyErrorMessage returns a human-readable error message for common errno values

func GetErrorMessage added in v0.1.1

func GetErrorMessage(errno int) string

GetErrorMessage returns a human-readable error message for common errno values

func GetMoveErrorMessage added in v0.1.1

func GetMoveErrorMessage(errno int) string

GetMoveErrorMessage returns a human-readable error message for common errno values

func GetRenameErrorMessage added in v0.1.1

func GetRenameErrorMessage(errno int) string

GetRenameErrorMessage returns a human-readable error message for common errno values

func GetSourceFileName added in v0.1.1

func GetSourceFileName(path string) string

GetSourceFileName extracts the filename from a path

func MapFileType

func MapFileType(isDir int) string

MapFileType converts the isdir field to a readable file type

func PrintError

func PrintError(message string)

PrintError prints an error message with consistent formatting

func PrintErrorAndExit

func PrintErrorAndExit(message string)

PrintErrorAndExit prints an error message and exits with code 1

func PrintSuccess

func PrintSuccess(message string)

PrintSuccess prints a success message with consistent formatting

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents a Baidu Pan client

func NewClient

func NewClient(clientID, clientSecret, tokenPath string) *Client

NewClient creates a new Baidu Pan client

func (*Client) Authorize

func (c *Client) Authorize(ctx context.Context) error

Authorize tries to load existing tokens first, and if not available or valid, performs device code authorization

func (*Client) CopyFile

func (c *Client) CopyFile(sourcePath, destPath string) error

CopyFile copies a single file or directory from source path to destination directory in Baidu Pan

func (*Client) CopyFiles

func (c *Client) CopyFiles(copyRequests []CopyRequest) error

CopyFiles copies multiple files based on the provided CopyRequest structs

func (*Client) CreateDir

func (c *Client) CreateDir(remotePath string) error

CreateDir creates a directory in Baidu Pan

func (*Client) DownloadFile

func (c *Client) DownloadFile(filePath string) (*http.Response, error)

DownloadFile downloads a file from Baidu Pan

func (*Client) DownloadFileToPath

func (c *Client) DownloadFileToPath(filePath, localPath string) error

DownloadFileToPath downloads a file from Baidu Pan and saves it to the specified local path

func (*Client) EnsureRemoteDirExists added in v0.1.1

func (c *Client) EnsureRemoteDirExists(remotePath string) error

EnsureRemoteDirExists verifies the remote directory path is valid. Baidu Pan's API typically creates parent directories if they don't exist during upload, so this function primarily serves to validate the path format.

func (*Client) GetAndDisplayFileInfo

func (c *Client) GetAndDisplayFileInfo(filePath string) (*FileInfo, error)

GetAndDisplayFileInfo gets file information from Baidu Pan and returns formatted information This encapsulates the logic for getting file info from Baidu Pan and processing it

func (*Client) GetDetailedFileInfo

func (c *Client) GetDetailedFileInfo(filePath string) (*FileInfo, error)

GetDetailedFileInfo gets detailed information about a file using the meta API This is more efficient than listing files when you only need info about one file

func (*Client) GetDeviceCode

func (c *Client) GetDeviceCode() (*DeviceCodeResponse, error)

GetDeviceCode initiates the device code flow

func (*Client) GetDeviceCodeForPoll

func (c *Client) GetDeviceCodeForPoll(deviceCode string) (*DeviceCodeResponse, error)

GetDeviceCodeForPoll gets device code info specifically for polling (internal use)

func (*Client) GetDiskInfo

func (c *Client) GetDiskInfo() (*DiskInfoResponse, error)

GetDiskInfo gets the user's cloud storage usage information

func (*Client) GetFileInfo

func (c *Client) GetFileInfo(filePath string) (*FileInfo, error)

GetFileInfo gets information about a specific file

func (*Client) GetFileInfoByPath

func (c *Client) GetFileInfoByPath(filePath string) (*FileInfo, error)

GetFileInfoByPath gets information about a specific file or directory by its path This method uses the list API with a filter to get information about a single file

func (*Client) HasRefreshToken

func (c *Client) HasRefreshToken() bool

HasRefreshToken returns whether the client has a refresh token available

func (*Client) HasValidToken

func (c *Client) HasValidToken() bool

HasValidToken checks if there's a valid token in the file

func (*Client) IsTokenExpired

func (c *Client) IsTokenExpired() bool

IsTokenExpired checks if the token is expired or will expire soon (within 2 days)

func (*Client) ListFiles

func (c *Client) ListFiles(dirPath string) ([]FileInfo, error)

ListFiles lists files in a directory

func (*Client) LoadTokens

func (c *Client) LoadTokens() error

LoadTokens loads the access token from a file

func (*Client) MoveFile

func (c *Client) MoveFile(sourcePath, destDir string) error

MoveFile moves a single file or directory from source path to destination directory in Baidu Pan

func (*Client) MoveFiles

func (c *Client) MoveFiles(moveRequests []MoveRequest) error

MoveFiles moves multiple files based on the provided MoveRequest structs

func (*Client) PollForToken

func (c *Client) PollForToken(deviceCode string) (*TokenResponse, error)

PollForToken polls the token endpoint until the user completes authorization

func (*Client) ReadFileContent

func (c *Client) ReadFileContent(filePath string) ([]byte, error)

ReadFileContent reads the content of a file from Baidu Pan

func (*Client) RefreshToken

func (c *Client) RefreshToken() error

RefreshToken attempts to refresh the access token using the refresh token

func (*Client) RemoveFile

func (c *Client) RemoveFile(filePath string) error

RemoveFile removes a single file or directory from Baidu Pan

func (*Client) RemoveFiles

func (c *Client) RemoveFiles(filePaths []string) error

RemoveFiles removes multiple files or directories from Baidu Pan

func (*Client) RenameFile

func (c *Client) RenameFile(sourcePath, newName string) error

RenameFile renames a single file or directory in Baidu Pan

func (*Client) RenameFiles

func (c *Client) RenameFiles(renameRequests []RenameRequest) error

RenameFiles renames multiple files based on the provided RenameRequest structs

func (*Client) SaveTokens

func (c *Client) SaveTokens() error

SaveTokens saves the access token to a file

func (*Client) UploadFile

func (c *Client) UploadFile(localFilePath, remoteFilePath string) error

UploadFile uploads a local file to Baidu Pan

func (*Client) Walk

func (c *Client) Walk(rootPath string) (<-chan FileInfo, <-chan error)

Walk recursively walks through directories and files

func (*Client) WalkRecursive added in v0.1.1

func (c *Client) WalkRecursive(path string, fileChan chan<- FileInfo, errChan chan<- error)

type CopyInfo

type CopyInfo struct {
	Path    string `json:"path"`
	Dest    string `json:"dest"`
	NewName string `json:"newname"`
	Errno   int    `json:"errno"`
}

CopyInfo represents the result for each copied file in the response

type CopyRequest

type CopyRequest struct {
	Path    string `json:"path"`
	Dest    string `json:"dest"`
	NewName string `json:"newname"`
}

CopyRequest represents the structure for a file to be copied

type CopyResponse

type CopyResponse struct {
	Errno     int        `json:"errno"`
	Info      []CopyInfo `json:"info"`
	TaskID    int64      `json:"taskid"`
	RequestID int64      `json:"request_id"`
}

CopyResponse represents the response from the copy API

type CreateFileResponse

type CreateFileResponse struct {
	Errno          int    `json:"errno"`
	FsID           int64  `json:"fs_id"`
	Path           string `json:"path"`
	CTime          int64  `json:"ctime"`
	MTime          int64  `json:"mtime"`
	MD5            string `json:"md5"`
	Size           int64  `json:"size"`
	Isdir          int    `json:"isdir"`
	Ifhassubdir    int    `json:"ifhassubdir"`
	Category       int    `json:"category"`
	ServerFilename string `json:"server_filename"`
	ParentPath     string `json:"parent_path"`
}

CreateFileResponse represents the response from the create file API

type DeleteEntry

type DeleteEntry struct {
	Path  string `json:"path"`
	Errno int    `json:"errno"`
}

DeleteEntry represents the result for each file in the delete operation

type DeleteResponse

type DeleteResponse struct {
	Errno     int           `json:"errno"`
	RequestID int64         `json:"request_id"`
	List      []DeleteEntry `json:"list"`
}

DeleteResponse represents the response from the delete API

type DeviceCodeResponse

type DeviceCodeResponse struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURL string `json:"verification_url"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
	QRCode          string `json:"qrcode,omitempty"`
}

DeviceCodeResponse represents the response from device code endpoint

type DiskInfoResponse

type DiskInfoResponse struct {
	Errno     int64 `json:"errno"`
	Total     int64 `json:"total"`  // Total space size in bytes
	Used      int64 `json:"used"`   // Used space size in bytes
	Free      int64 `json:"free"`   // Free capacity in bytes
	Expire    bool  `json:"expire"` // Whether capacity will expire within 7 days
	RequestID int64 `json:"request_id"`
}

DiskInfoResponse represents the response from the disk info API

type FileInfo

type FileInfo struct {
	TkBindID       int    `json:"tkbind_id"`
	OwnerType      int    `json:"owner_type"`
	Category       int    `json:"category"`
	IsScene        int    `json:"is_scene"`
	FsID           int64  `json:"fs_id"`
	BlackTag       int    `json:"black_tag"`
	ServerFilename string `json:"server_filename"`
	ExtentInt2     int    `json:"extent_int2"`
	ServerAtime    int64  `json:"server_atime"`
	ServerCtime    int64  `json:"server_ctime"`
	ExtentInt8     int    `json:"extent_int8"`
	WpFile         int    `json:"wpfile"`
	Unlist         int    `json:"unlist"`
	LocalMtime     int64  `json:"local_mtime"`
	Size           int64  `json:"size"`
	OperID         int64  `json:"oper_id"`
	OwnerID        int64  `json:"owner_id"`
	Share          int    `json:"share"`
	FromType       int    `json:"from_type"`
	Path           string `json:"path"`
	LocalCTime     int64  `json:"local_ctime"`
	PL             int    `json:"pl"`
	RealCategory   string `json:"real_category"`
	IsDir          int    `json:"isdir"`
	ExtentTinyInt7 int    `json:"extent_tinyint7"`
	ServerMtime    int64  `json:"server_mtime"`
	MD5            string `json:"md5,omitempty"`
}

FileInfo represents a file or directory in Baidu Pan

type ListFilesResponse

type ListFilesResponse struct {
	Errno     int        `json:"errno"`
	GuidInfo  string     `json:"guid_info"`
	List      []FileInfo `json:"list"`
	RequestID int64      `json:"request_id"`
	Guid      int        `json:"guid"`
}

ListFilesResponse represents the response from the list files API

type MoveInfo

type MoveInfo struct {
	Path    string `json:"path"`
	Dest    string `json:"dest"`
	NewName string `json:"newname"`
	Errno   int    `json:"errno"`
}

MoveInfo represents the result for each moved file in the response

type MoveRequest

type MoveRequest struct {
	Path    string `json:"path"`
	Dest    string `json:"dest"`
	NewName string `json:"newname"`
}

MoveRequest represents the structure for a file to be moved

type MoveResponse

type MoveResponse struct {
	Errno     int        `json:"errno"`
	Info      []MoveInfo `json:"info"`
	TaskID    int64      `json:"taskid"`
	RequestID int64      `json:"request_id"`
}

MoveResponse represents the response from the move API

type PrecreateResponse

type PrecreateResponse struct {
	Errno      int    `json:"errno"`
	UploadID   string `json:"uploadid"`
	BlockList  []int  `json:"block_list"`
	RequestID  int64  `json:"request_id"`
	ReturnType int    `json:"return_type"` // 1: need upload, 2: no need upload (file already exists and matches)
}

PrecreateResponse represents the response from the precreate API

type ProgressWriter

type ProgressWriter struct {
	// contains filtered or unexported fields
}

ProgressWriter wraps an io.Writer and reports progress

func (*ProgressWriter) Write

func (pw *ProgressWriter) Write(p []byte) (int, error)

type RenameInfo

type RenameInfo struct {
	Path  string `json:"path"`
	Errno int    `json:"errno"`
}

RenameInfo represents the result for each renamed file in the response

type RenameRequest

type RenameRequest struct {
	Path    string `json:"path"`
	NewName string `json:"newname"`
}

RenameRequest represents the structure for a file to be renamed

type RenameResponse

type RenameResponse struct {
	Errno     int          `json:"errno"`
	Info      []RenameInfo `json:"info"`
	TaskID    int64        `json:"taskid"`
	RequestID int64        `json:"request_id"`
}

RenameResponse represents the response from the rename API

type TokenFile

type TokenFile struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token"`
	ExpiresIn    int       `json:"expires_in"`
	UID          string    `json:"uid"`
	CreatedAt    time.Time `json:"created_at"` // Time when token was obtained
}

TokenFile represents the structure for storing tokens in a file

type TokenResponse

type TokenResponse struct {
	AccessToken   string `json:"access_token"`
	RefreshToken  string `json:"refresh_token"`
	ExpiresIn     int    `json:"expires_in"`
	Scope         string `json:"scope"`
	SessionKey    string `json:"session_key"`
	SessionSecret string `json:"session_secret"`
	UID           string `json:"uid"`
}

TokenResponse represents the access token response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL