/// <summary> /// 根据访问或者文件的URL获得所属网盘类型 /// </summary> /// <param name="url"></param> /// <returns></returns> private CloudDiskType GetDiskTypeByURL(string url) { CloudDiskType type = CloudDiskType.NOT_SPECIFIED; //if (url.Contains("sina")) //{ // type = CloudDiskType.SINA; //} return(type); }
/// <summary> /// 判断一个网盘是否被正常加载 /// </summary> /// <param name="type"></param> /// <returns></returns> public bool IsDiskTypeLoaded(CloudDiskType type, out ICloudDiskAPI api) { api = null; foreach (ICloudDiskAPI one in _loadedCloudDiskApi) { if (one.GetDiskType() == type) { api = one; return(true); } } return(false); }
/// <summary> /// 下载一个文件.有可能会抛出异常,异常在外面处理 /// </summary> /// <param name="remotePath">远程文件地址</param> /// <returns>文件字节内容</returns> public byte[] DownloadFile(CloudDiskType type, string remotePath) { byte[] fileContent = null; try { //所有传入的路径都进行一次转换 remotePath = PathConverter.LocalPathToRemotePath(remotePath); //CloudDiskType type = GetDiskTypeByURL(remotePath); if (type != CloudDiskType.NOT_SPECIFIED) { ICloudDiskAPI oneApi; if (IsDiskTypeLoaded(type, out oneApi)) { fileContent = oneApi.DownloadFile(remotePath); } else { //throw new Exception("文件地址不正确或者网盘接口模块没有被正确加载!"); return(null); } } else { foreach (ICloudDiskAPI api in _loadedCloudDiskApi) { if (api.GetCloudFileInfo(remotePath) != null) { fileContent = api.DownloadFile(remotePath); break; } } } } catch (Exception ex) { throw new Exception("获取远程文件信息出错!" + ex.Message); } return(fileContent); }
/// <summary> /// 移动远程文件 /// </summary> /// <param name="originPath">原始路径</param> /// <param name="newPath">新路径</param> /// <param name="isCopy">是否需要复制</param> /// <returns>新文件的信息</returns> public CloudFileInfoModel MoveFile(CloudDiskType type, string originPath, string newPath, bool isCopy) { CloudFileInfoModel m = new CloudFileInfoModel(); try { //CloudDiskType type = GetDiskTypeByURL(originPath); ICloudDiskAPI oneApi; if (IsDiskTypeLoaded(type, out oneApi)) { oneApi.MoveFile(originPath, newPath, isCopy); } else { throw new Exception("文件地址不正确或者网盘接口模块没有被正确加载!"); } } catch (Exception ex) { throw new Exception("移动远程文件信息出错!" + ex.Message); } return(m); }
/// <summary> /// 删除文件 /// </summary> /// <param name="remotePath"></param> /// <returns></returns> public int DeleteFile(CloudDiskType type, string remotePath) { try { //CloudDiskType type = GetDiskTypeByURL(remotePath); //所有传入的路径都进行一次转换 remotePath = PathConverter.LocalPathToRemotePath(remotePath); ICloudDiskAPI oneApi; if (type != CloudDiskType.NOT_SPECIFIED) { if (IsDiskTypeLoaded(type, out oneApi)) { oneApi.DeleteFile(remotePath); } else { throw new Exception("文件地址不正确或者网盘接口模块没有被正确加载!"); } } else { foreach (ICloudDiskAPI api in _loadedCloudDiskApi) { if (api.GetCloudFileInfo(remotePath) != null) { api.DeleteFile(remotePath); } } } } catch (Exception ex) { throw new Exception("删除远程文件信息出错!" + ex.Message); } return(1); }
/// <summary> /// 获取一个远程文件的信息 /// </summary> /// <param name="type">网盘类型</param> /// <param name="isGetDirectory">是否是获取文件夹,否的话表示获取文件</param> /// <param name="remotePath">远程文件的相对路径</param> /// <returns></returns> public CloudFileInfoModel GetCloudFileInfo(CloudDiskType type, bool isGetDirectory, string remotePath) { CloudFileInfoModel m = null; try { //CloudDiskType type = GetDiskTypeByURL(remotePath); //所有传入的文件都进行一次转换 remotePath = PathConverter.LocalPathToRemotePath(remotePath); if (isGetDirectory) { //是文件夹,需要遍历所有可用网盘,将所有子文件和子文件夹信息加总一起反馈 foreach (ICloudDiskAPI api in _loadedCloudDiskApi) { var tmp_dir = api.GetCloudFileInfo(remotePath); if (tmp_dir != null) { if (m == null) { m = new CloudFileInfoModel(); m.Path = tmp_dir.Path; m.name = tmp_dir.name; m.IsDir = tmp_dir.IsDir; m.LastModifiedDate = tmp_dir.LastModifiedDate; m.Contents = new List <CloudFileInfoModel>(); } //将每个找到网盘的子目录和子文件都加入到返回值m里去. if (tmp_dir.Contents != null) { foreach (CloudFileInfoModel subDir in tmp_dir.Contents) { m.Contents.Add(subDir); } } } } } else { //是文件需要找到是哪个网盘 if (type == CloudDiskType.NOT_SPECIFIED) { //未指定类型,必须便利查找属于哪个网盘 foreach (ICloudDiskAPI api in _loadedCloudDiskApi) { m = api.GetCloudFileInfo(remotePath); if (m != null) { //第一个找到就返回 break; } } if (m == null) { //全部遍历了依然没有找到 throw new Exception("文件地址不正确或者网盘接口模块没有被正确加载!"); } } else { //指定类型的 ICloudDiskAPI oneApi; if (IsDiskTypeLoaded(type, out oneApi)) { m = oneApi.GetCloudFileInfo(remotePath); } else { throw new Exception("文件地址不正确或者网盘接口模块没有被正确加载!"); } } } } catch (Exception ex) { throw new Exception("获取远程文件信息出错!" + ex.Message); } //在传出返回值之前将远程路径的形式替换成本地路径 if (m != null) { if (m.Path != null) { m.LocalPath = PathConverter.RemotePathToLocalPath(m.Path); } } return(m); }