openlist

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 14 Imported by: 0

README

OpenList Go Client

OpenList Go Client 是一个用于与 OpenList 文件管理服务进行交互的 Go 语言客户端库。它提供了简洁的 API 来执行文件上传、下载、搜索、删除和管理等操作。

功能特性

  • 🔐 用户认证:自动处理登录和令牌管理
  • 📁 文件管理:上传、下载、删除、获取文件信息、列出目录内容
  • 🔍 文件搜索:根据关键词搜索文件
  • 🔄 备份管理:自动备份目录并保留最新3份备份
  • 🌐 代理支持:可配置 HTTP 代理
  • 🔄 自动重试:登录状态自动维护
  • 📦 易于集成:简洁的 API 设计,易于集成到您的 Go 项目中

安装

确保您已经安装了 Go 1.16 或更高版本。

go get -u github.com/littleboss01/openlistClient

或者在您的项目目录中初始化 Go 模块:

go mod init your-project-name
go get github.com/littleboss01/openlistClient

快速开始

package main

import (
    "fmt"
    "log"
    "openlist"
)

func main() {
    // 创建客户端实例
    api := openlist.NewOpenListAPI(
        "http://localhost:5244", // OpenList服务地址
        "admin",                 // 用户名
        "123456",                // 密码
        "",                      // 代理地址(可选)
    )

    // 登录
    if ok, err := api.Login(); !ok {
        log.Fatal("登录失败:", err)
    }

    // 上传文件
    remotePath, err := api.UploadFile("/local/path/test.txt", "/remote/docs")
    if err != nil {
        log.Fatal("文件上传失败:", err)
    }
    fmt.Printf("文件上传成功,远程路径: %s\n", remotePath)

    // 获取文件信息
    fileInfo, err := api.GetFileInfo(remotePath)
    if err != nil {
        log.Fatal("获取文件信息失败:", err)
    }
    fmt.Printf("文件大小: %d字节,下载地址: %s\n", fileInfo.Size, fileInfo.URL)
}

API 参考

创建客户端
api := openlist.NewOpenListAPI(baseURL, username, password, proxy)
登录
ok, err := api.Login()
上传文件
remotePath, err := api.UploadFile(localFilePath, remoteDirectory)
下载文件(带进度回调)
// 定义进度回调函数
progressFunc := func(downloaded, total int64) {
    fmt.Printf("下载进度: %d/%d bytes\n", downloaded, total)
}

// 下载文件
err := api.DownloadFile(remoteFilePath, localFilePath, progressFunc)
删除文件或文件夹
// 删除单个文件
err := api.Remove("/remote/docs", []string{"test.txt"})

// 删除多个文件
err := api.Remove("/remote/docs", []string{"test1.txt", "test2.txt"})

// 删除文件夹
err := api.Remove("/remote", []string{"docs"})
备份目录并保留最新3份
// 备份目录并自动管理备份文件
// 该功能会上传新的备份文件,并自动删除旧的备份文件,只保留最新的3份
err := backupExample() // 参见test/backup_example.go
检测目录并下载最新版本
// 检测目录中的版本文件,找出最新版本并下载
err := versionCheckExample() // 参见test/version_check_example.go
获取文件信息
fileInfo, err := api.GetFileInfo(filePath)
搜索文件
results, err := api.SearchFiles(keyword, parentPath)
列出目录内容
listResp, err := api.ListFiles(path, page, perPage, refresh)

错误处理

所有 API 方法都会返回详细的错误信息,您可以根据需要进行处理:

if _, err := api.Login(); err != nil {
    // 处理登录错误
    log.Printf("登录失败: %v", err)
}

许可证

MIT License

参考

https://openlist.apifox.cn/ https://github.com/OpenListTeam/OpenList

贡献

欢迎提交 Issue 和 Pull Request 来改进这个项目。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIResponse

type APIResponse struct {
	Code    int         `json:"code"`    // 状态码(200为成功)
	Message string      `json:"message"` // 描述信息
	Data    interface{} `json:"data"`    // 业务数据(动态解析)
}

APIResponse 通用API响应结构体(用于解析非登录接口的返回)

type FileInfo

type FileInfo struct {
	Name      string      `json:"name"`     // 文件名
	Size      int64       `json:"size"`     // 文件大小(字节)
	IsDir     bool        `json:"is_dir"`   // 是否为目录
	Modified  time.Time   `json:"modified"` // 修改时间
	Created   time.Time   `json:"created"`
	Sign      string      `json:"sign"`
	Thumb     string      `json:"thumb"`
	Type      int64       `json:"type"`
	HashInfo  interface{} `json:"hashinfo"`
	Hash_info interface{} `json:"hash_info"`

	//list没有get才有
	Raw_url string      `json:"raw_url"`
	Related interface{} `json:"related"`
}

FileInfo 文件信息结构体(对应原Python的Dict返回)

type FileInfoRequest

type FileInfoRequest struct {
	Path     string `json:"path"`
	Password string `json:"password"`
}

FileInfoRequest 获取文件信息请求参数

type HTTPRequest

type HTTPRequest struct {
	Method  string            // HTTP方法 (GET, POST, PUT, DELETE等)
	URL     string            // 请求URL
	Body    interface{}       // 请求体数据(会自动序列化为JSON)
	Headers map[string]string // 请求头
}

HTTPRequest 通用HTTP请求配置

type ListRequest

type ListRequest struct {
	Path     string `json:"path"`
	Password string `json:"password"`
	Page     int    `json:"page"`
	PerPage  int    `json:"per_page"`
	Refresh  bool   `json:"refresh"`
}

ListRequest 列表请求参数

type ListResponse

type ListResponse struct {
	Content  []FileInfo `json:"content"`  // 文件/目录列表
	Total    int        `json:"total"`    // 总数量
	Page     int        `json:"page"`     // 当前页码
	PerPage  int        `json:"per_page"` // 每页条数
	Write    bool       `json:"write"`
	Provider string     `json:"provider"`
	Readme   string     `json:"readme"`
	Header   string     `json:"header"`
}

ListResponse 目录列表响应结构体

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

LoginRequest 登录请求参数

type LoginResponse

type LoginResponse struct {
	Token string `json:"token"` // 登录令牌
}

LoginResponse 登录接口响应结构体

type MkdirRequest added in v1.0.4

type MkdirRequest struct {
	Path string `json:"path"` // 新目录路径
}

MkdirRequest 创建文件夹请求参数

type OpenListAPI

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

OpenListAPI OpenList 服务客户端

func NewOpenListAPI

func NewOpenListAPI(baseURL, username, password, proxy string) *OpenListAPI

NewOpenListAPI 创建OpenListAPI客户端实例

func (*OpenListAPI) DownloadFile

func (c *OpenListAPI) DownloadFile(remotePath, localPath string, progressFunc ProgressFunc) error

DownloadFile 从OpenList服务下载文件 remotePath: 远程文件路径(如 "/docs/test.txt") localPath: 本地保存路径 progressFunc: 进度回调函数(可选) 返回值: 错误信息

func (*OpenListAPI) GetFileInfo

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

GetFileInfo 获取文件信息(含下载地址) filePath: 远程文件路径(如 "/docs/test.txt") 返回值: 文件信息结构体,错误信息

func (*OpenListAPI) ListFiles

func (c *OpenListAPI) ListFiles(path string, page, perPage int, refresh bool) (*ListResponse, error)

ListFiles 列出目录下的文件/目录 path: 目录路径(默认 "/") page: 页码(默认 1) perPage: 每页条数(0表示不分页) refresh: 是否强制刷新(默认 true) 返回值: 目录列表响应,错误信息

func (*OpenListAPI) Login

func (c *OpenListAPI) Login() (bool, error)

Login 登录OpenList服务,获取访问令牌

func (*OpenListAPI) Mkdir added in v1.0.4

func (c *OpenListAPI) Mkdir(path string) error

Mkdir 创建文件夹 path: 新目录路径 返回值: 错误信息

func (*OpenListAPI) Mkdirs added in v1.0.4

func (c *OpenListAPI) Mkdirs(path string) error

Mkdir 创建文件夹 path: 新目录路径 返回值: 错误信息

func (*OpenListAPI) Remove

func (c *OpenListAPI) Remove(dir string, names []string) error

Remove 删除文件或文件夹 dir: 目录路径 names: 要删除的文件或文件夹名称列表 返回值: 错误信息

func (*OpenListAPI) ResetProxyStatus

func (c *OpenListAPI) ResetProxyStatus()

ResetProxyStatus 重置代理状态(修改代理配置后调用,重新测试)

func (*OpenListAPI) SearchFiles

func (c *OpenListAPI) SearchFiles(keyword, parentPath string) (*SearchResult, error)

SearchFiles 搜索文件 keyword: 搜索关键词 parentPath: 搜索父目录(默认 "/") 返回值: 搜索结果列表,错误信息

func (*OpenListAPI) TestProxy

func (c *OpenListAPI) TestProxy() bool

TestProxy 测试代理是否可用

func (*OpenListAPI) UploadFile

func (c *OpenListAPI) UploadFile(filePath, remotePath string) (string, error)

UploadFile 上传文件到OpenList服务 filePaths: 本地文件路径 remotePath: 远程存储目录(如 "/docs") 返回值: 远程文件完整路径(如 "/docs/test.txt"),错误信息

type ProgressFunc

type ProgressFunc func(downloaded, total int64)

ProgressFunc 进度回调函数类型 参数: 已下载字节数, 总字节数

type ProgressReader

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

ProgressReader 带进度回调的Reader

func (*ProgressReader) Read

func (pr *ProgressReader) Read(p []byte) (int, error)

Read 实现io.Reader接口

type RemoveRequest

type RemoveRequest struct {
	Dir   string   `json:"dir"`   // 目录
	Names []string `json:"names"` // 文件名列表
}

RemoveRequest 删除文件或文件夹请求参数

type SearchRequest

type SearchRequest struct {
	Parent   string `json:"parent"`
	Keywords string `json:"keywords"`
	Scope    int    `json:"scope" default:"0"`

	Page     int `json:"page" default:"1"`
	Per_page int `json:"per_page" default:"50"`
}

SearchRequest 搜索文件请求参数

type SearchResult

type SearchResult struct {
	Content []struct {
		Parent string `json:"parent"`
		Name   string `json:"name"`   // 文件名
		IsDir  bool   `json:"is_dir"` // 是否为目录
		Size   int64  `json:"size"`   // 文件大小(字节)
		Type   int64  `json:"type"`
	}
}

SearchResult 搜索结果结构体

type UploadRequest

type UploadRequest struct {
	FilePath   string `json:"file_path"`
	RemotePath string `json:"remote_path"`
}

UploadRequest 上传文件请求参数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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