代码:
public class KodCloudHelper
{
static readonly string kodCloudUrl = Configs.GetString("KodCloudUrl");
static readonly string kodCloudUser = Configs.GetString("KodCloudUser");
static readonly string kodCloudPwd = Configs.GetString("KodCloudPwd");
private readonly HttpClient Client;//如果不是一个client实例 可道云需要多传一个accessToken参数
public KodCloudHelper()
{
Client = new HttpClient();
}
/// <summary>
/// 获取accessToken
/// </summary>
/// <returns></returns>
public async Task<string> GetAccessToken()
{
var requestUrl = $"{kodCloudUrl}?user/loginSubmit&isAjax=1&getToken=1&name={kodCloudUser}&password={kodCloudPwd}";
return await GetKodApi(requestUrl);
}
/// <summary>
/// 获取文件列表
/// </summary>
/// <param name="path">文件夹路径</param>
/// <returns></returns>
public async Task<string> GetFileList(string path)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathList&path={path}";
return await GetKodApi(requestUrl);
}
/// <summary>
/// 批量获取文件属性
/// </summary>
/// <param name="getFilesAttrModel"></param>
/// <returns></returns>
public async Task<string> GetFileAttr(List<GetFilesAttrModel> getFilesAttrModels)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathInfo";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"dataArr", JsonConvert.SerializeObject(getFilesAttrModels)}
});
}
/// <summary>
/// 指定路径创建文件夹
/// </summary>
/// <param name="dirPath"></param>
/// <returns></returns>
public async Task<string> Mkdir(string dirPath)
{
var requestUrl = $"{kodCloudUrl}?explorer/mkdir";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"path", dirPath}
});
}
/// <summary>
/// 重命名文件或文件夹
/// </summary>
/// <param name="dirPath"></param>
/// <param name="newDirPath"></param>
/// <returns></returns>
public async Task<string> ReNameDir(string dirPath, string newDirPath)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathRname";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"path", dirPath},
{"rnameTo",newDirPath }
});
}
/// <summary>
/// 批量移动文件或文件夹
/// </summary>
/// <param name="moveFileModels">要移动的文件</param>
/// <param name="targetPath">要移动的地址</param>
/// <returns></returns>
public async Task<string> MoveFiles(List<MoveFileModel> moveFileModels,string targetPath)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathCuteDrag";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"dataArr", JsonConvert.SerializeObject(moveFileModels)},
{"path",targetPath}
});
}
/// <summary>
/// 批量复制文件或文件夹
/// </summary>
/// <param name="copyFileModels"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public async Task<string> CopyFiles(List<CopyFileModel> copyFileModels, string targetPath)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathCopyDrag";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"dataArr", JsonConvert.SerializeObject(copyFileModels)},
{ "path",targetPath}
});
}
/// <summary>
/// 批量删除文件或文件夹
/// </summary>
/// <param name="deleteFileModels"></param>
/// <param name="shiftDelete">true代表彻底删除 false代表移动到回收站</param>
/// <returns></returns>
public async Task<string> DeleteFiles(List<DeleteFileModel> deleteFileModels,bool shiftDelete=false)
{
var requestUrl = $"{kodCloudUrl}?explorer/pathDelete";
var postDataDic = new Dictionary<string, string>()
{
{"dataArr", JsonConvert.SerializeObject(deleteFileModels)}
};
if (shiftDelete)
{
postDataDic.Add("shiftDelete", "1");
}
return await PostKodApi(requestUrl, postDataDic);
}
/// <summary>
/// 获取文件内容
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public async Task<string> GetFileContent(string fileName)
{
var requestUrl = $"{kodCloudUrl}?editor/fileGet";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"filename", fileName}
});
}
/// <summary>
/// 保存文件内容
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileStr">保存的内容字符串</param>
/// <param name="charSet">编码方式</param>
/// <returns></returns>
public async Task<string> SaveFileContent( string fileName,string fileStr,string charSet="utf-8")
{
var requestUrl = $"{kodCloudUrl}?editor/fileSave";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"path", fileName},
{"charset", charSet},
{"filestr",fileStr}
});
}
/// <summary>
/// 上传文件到可道云
/// </summary>
/// <param name="fullPath"></param>
/// <param name="tarGetFullPath"></param>
/// <returns></returns>
public async Task<bool> UploadFile(string fullPath, string tarGetFullPath)
{
var requestUrl = $"{kodCloudUrl}?explorer/fileUpload";
return await PostFileToKod(requestUrl, fullPath, tarGetFullPath);
}
/// <summary>
/// 下载可道云文件
/// </summary>
/// <param name="apiUrl">要获取的文件地址</param>
/// <param name="filePath">要储存的路径</param>
/// <param name="filename">要储存的文件名</param>
/// <returns></returns>
public async Task<bool> DownLoadFile(string targetFile, string filePath, string filename)
{
var requestUrl = $"{kodCloudUrl}?explorer/fileDownload&path={targetFile}";
return await GetFileToKod(requestUrl,filePath,filename);
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileType">要压缩文件格式;格式支持zip、tar、gz</param>
/// <param name="zipFileModel">要压缩的文件列表</param>
/// <returns></returns>
public async Task<string> ZipFile(List<ZipFileModel> zipFileModels, string fileType="zip")
{
var requestUrl = $"{kodCloudUrl}?explorer/zip";
return await PostKodApi(requestUrl, new Dictionary<string, string>()
{
{"dataArr", JsonConvert.SerializeObject(zipFileModels)},
{"fileType",fileType}
});
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="targetFile">要解压的文件</param>
/// <param name="targetFilePath">解压到(不传默认解压到当前文件夹)</param>
/// <returns></returns>
public async Task<string> UnZipFile(string targetFile,string targetFilePath="")
{
var requestUrl = $"{kodCloudUrl}?explorer/unzip";
var postDataDic = new Dictionary<string, string>()
{
{"path", targetFile}
};
if (!string.IsNullOrEmpty(targetFilePath))
{
postDataDic.Add("pathTo", targetFilePath);
}
return await PostKodApi(requestUrl, postDataDic);
}
#region http请求
/// <summary>
/// get方式调用可道云api
/// </summary>
/// <param name="apiUrl"></param>
/// <returns></returns>
internal async Task<string> GetKodApi(string apiUrl)
{
var requestUrl = apiUrl;
var response = await Client.GetAsync(requestUrl).ConfigureAwait(false);
string result = await response.Content.ReadAsStringAsync();
var resultRecord = Record.Parse(result);
if (resultRecord.Get("code", true) is bool resultCode && resultCode)
{
return resultRecord.GetString("data");
}
return "";
}
/// <summary>
/// post方式请求可道云
/// </summary>
/// <param name="apiUrl"></param>
/// <param name="postDataDic"></param>
/// <returns></returns>
internal async Task<string> PostKodApi(string apiUrl, Dictionary<string,string> postDataDic)
{
var requestUrl = apiUrl;
HttpContent httpContent = new FormUrlEncodedContent(postDataDic);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await Client.PostAsync(requestUrl, httpContent).ConfigureAwait(false);
string result = await response.Content.ReadAsStringAsync();
var resultRecord = Record.Parse(result);
if (resultRecord.Get("code", true) is bool resultCode && resultCode)
{
return resultRecord.GetString("data");
}
return "";
}
/// <summary>
/// post文件到可道云
/// </summary>
/// <param name="fullPath">要上传的文件路径</param>
/// <param name="tarGetFullPath">目标文件夹路径</param>
/// <returns></returns>
internal async Task<bool> PostFileToKod(string apiUrl,string fullPath,string tarGetFullPath)
{
string reqUrl = apiUrl;
string boundary = string.Format("----WebKitFormBoundary{0}", DateTime.Now.Ticks.ToString("x"));
MultipartFormDataContent content = new MultipartFormDataContent(boundary);
//content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
if (string.IsNullOrEmpty(fullPath) && !File.Exists(fullPath))
{
return false;
}
string fileName = Path.GetFileName(fullPath);
FileStream fStream = File.Open(fullPath, FileMode.Open, FileAccess.Read);
content.Add(new StreamContent(fStream, (int)fStream.Length), "file", fileName);
content.Add(new StringContent(tarGetFullPath), "upload_to");
content.Add(new StringContent(fileName), "name");
var result = await Client.PostAsync(reqUrl, content);
try
{
if (result.IsSuccessStatusCode)
{
string rslt = result.Content.ReadAsStringAsync().Result;
var resultRecord = Record.Parse(rslt);
if (resultRecord.Get("code", true) is bool resultCode)
{
return resultCode;
}
return true;
}
}
catch (Exception ex)
{
}
finally
{
fStream.Close();
Client.Dispose();
}
return false;
}
/// <summary>
/// get方式获取可道云文件
/// </summary>
/// <param name="apiUrl">要获取的文件地址</param>
/// <param name="filePath">要储存的路径</param>
/// <param name="filename">要储存的文件名</param>
/// <returns></returns>
internal async Task<bool> GetFileToKod(string apiUrl, string filePath, string filename)
{
var url = apiUrl;
FileStream fs;
Client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");//添加自主驱动 很重要,没细研究,确实添加上就能下载文件
Client.DefaultRequestHeaders.Add("Accept", @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");//添加数据格式
var bytes =await Client.GetByteArrayAsync(url);
if (File.Exists(filePath + "//" + filename))
{
File.Delete(filePath + "//" + filename);
}
fs = new FileStream(filePath + "//" + filename, FileMode.CreateNew);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
return true;
}
#endregion
}
实体类:
public class CopyFileModel
{
/// <summary>
/// 要复制的类型 file folder
/// </summary>
public string type { get; set; }
/// <summary>
/// 要复制的目标文件全路径
/// </summary>
public string path { get; set; }
}
public class DeleteFileModel
{
/// <summary>
/// 要删除的类型 file folder
/// </summary>
public string type { get; set; }
/// <summary>
/// 要删除的目标文件全路径
/// </summary>
public string path { get; set; }
}
public class GetFilesAttrModel
{
/// <summary>
/// 要获取的类型 file folder
/// </summary>
public string type { get; set; }
/// <summary>
/// 要获取文件的全路径
/// </summary>
public string path { get; set; }
}
public class MoveFileModel
{
/// <summary>
/// 要移动的类型 file folder
/// </summary>
public string type { get; set; }
/// <summary>
/// 要移动的目标文件全路径
/// </summary>
public string path { get; set; }
}
public class ZipFileModel
{
/// <summary>
/// 要添加到压缩包的文件类型 file folder
/// </summary>
public string type { get; set; }
/// <summary>
/// 要添加到压缩包的文件目标文件全路径
/// </summary>
public string path { get; set; }
}
//dataArr: [{"type":"folder","path":"/test/folder1/"},{"type":"folder","path":"/test/folder2/af.txt"}]
// 创建文件名为dataArr中第一个path 追加压缩文件扩展名;例如上面的就会压缩成/test/folder1.zip
c#操作可道云api帮助类的更多相关文章
-
ShowDoc,APIDoc,可道云API,语雀-适合IT企业的文档工具
ShowDoc,APIDoc,可道云API,语雀-适合IT企业的文档工具 一.ShowDoc官方文档及说明 1.1 它可以用来做什么 1.2 它都有些什么功能 1.3 使用在线的ShowDoc 1.4 ...
-
腾讯云linux+kodexplorer可道云搭建私有云盘
kodexplorer可道云介绍KodExplorer可道云,原名芒果云,是基于Web技术的私有云和在线文件管理系统.致力于为用户提供安全可控.可靠易用.高扩展性的私有云解决方案.用户只需通过简单环境 ...
-
[Python] 使用有道翻译API
Python 使用youdao (有道翻译)API 想写一个给自己记录背单词状况的软件,需要获取英文单词的中文释义(基本功能).考虑使用有道翻译的API实现获取英文单词的中文释义的方法. 获取API_ ...
-
阿里云API网关(11)API的三种安全认证方式
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
-
阿里云API网关(9)常见问题
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
-
阿里云API网关(6)用户指南(开放 API )
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
-
又拍云 API 使用的那些小事
又拍云提供了丰富的 API 调用,为了减少用户在初次接入时可能会遇到的坑”,本文将对又拍云常用的 API 使用方法做个简单的梳理,力求让业务接入变得更简单,更高效. 目前我们的 API 主要有四大类, ...
-
禅道 Rest API 开发
在老的 PHP 系统中使用 PHP 5.3以后的库 所谓老的系统,是指没有使用PHP 5.3以上命名空间(namespace)特性编码的系统. 但是,只要你的系统运行在 PHP 5.3及以上的环境,在 ...
-
openstack操作之二 restful api
Restful api 是openstack各服务调用的接口,简单理解为可以通过网络去调用的函数.postman是一款前端调用工具,测试后端接口的时候往往是使用该工具去验证.在openstack的使用 ...
随机推荐
-
利用React实现表头维度功能
这是我真正意义上地用react实现一些东西.这次分享的是一个很简单的小组件,效果图先放上来: 前端样式用的是一套框架.功能很简单,就是根据选择的维度,在成员里选择对应这个维度的选项. 首先初始化一些数 ...
-
Snipaste
http://files.cnblogs.com/files/hwd13/Snipast.zip
-
HDU 1074
http://acm.hdu.edu.cn/showproblem.php?pid=1074 每个任务有一个截止日期和完成时间,超过截止日期一天扣一分,问完成全部任务最少扣几分,并输出路径 最多15个 ...
-
Spring MVC Checkbox And Checkboxes Example
In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...
-
View inflate方法和LayoutInflater inflate方法的区别详解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...
-
MyEclipse 10.x中拓展自动提示功能
原文转自:MyEclipse 10.7中拓展自动提示功能 在myeclipse 9以前的版本中,我们如果要为html编辑器添加自动的代码提示可以这样操作: 1.windows-->prefere ...
-
【Unity3D与23种设计模式】工厂方法模式(Factory Method)
GoF中定义: "定义一个可以产生对象的接口,但是让子类决定要产生哪一个类的对象.工厂方法模式让类的实例化程序延迟到子类中实施" 当类的对象产生时,若出现下列情况: 1.需要复杂的 ...
-
python之抽象基类
抽象基类特点 1.不能够实例化 2.在这个基础的类中设定一些抽象的方法,所有继承这个抽象基类的类必须覆盖这个抽象基类里面的方法 思考 既然python中有鸭子类型,为什么还要使用抽象基类? 一是我们在 ...
-
[UFLDL] Basic Concept
博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html 参考资料: UFLDL wiki UFLDL St ...
-
Please restart this script from an administrative PowerShell
问题 在上一篇<MSBUILD : error MSB3428: 未能加载 Visual C++ 组件"VCBuild.exe">,安装时失败了,提示: 解决办法 1. ...