Exemplo n.º 1
0
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="remotePath"></param>
        /// <returns></returns>
        public int DeleteDirectory(string remotePath)
        {
            //所有传入的路径都进行一次转换
            remotePath = PathConverter.LocalPathToRemotePath(remotePath);

            foreach (ICloudDiskAPI api in _loadedCloudDiskApi)
            {
                api.DeleteDirectory(remotePath);
            }
            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 上传一个文件,具体上传至哪个网盘由内部决定,有可能会抛出异常,异常在外面处理
        /// </summary>
        /// <param name="type">上传类型,新增或者更新</param>
        /// <param name="filePath">文件相对路径,含文件名</param>
        /// <param name="fileContent">文件内容</param>
        /// <returns></returns>
        public CloudFileInfoModel UploadFile(CloudFileUploadType type, string filePath, byte[] fileContent)
        {
            CloudFileInfoModel uploaded = new CloudFileInfoModel();

            //所有传入的路径都进行一次转换
            filePath = PathConverter.LocalPathToRemotePath(filePath);

            if (type == CloudFileUploadType.Create)
            {
                List <ICloudDiskAPI> apis = GetOptimizedDisk();
                if (apis != null)
                {
                    foreach (ICloudDiskAPI oneApi in apis)
                    {
                        uploaded = oneApi.UploadFile(fileContent, filePath);
                    }
                }
                else
                {
                    //throw new Exception("没有可用的网盘可供上传!");
                    return(null);
                }
            }
            //修改
            if (type == CloudFileUploadType.Update)
            {
                //遍历所有网盘
                foreach (ICloudDiskAPI api in _loadedCloudDiskApi)
                {
                    if (api.GetCloudFileInfo(filePath) != null)
                    {
                        uploaded = api.UploadFile(fileContent, filePath);
                    }
                }
            }
            //在传出返回值之前将远程路径的形式替换成本地路径
            if (uploaded != null)
            {
                uploaded.LocalPath = PathConverter.RemotePathToLocalPath(uploaded.Path);
            }
            return(uploaded);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 创建一个远程文件夹,在所有网盘的所有相同的相对路径下面创建一个一模一样的文件夹
        /// </summary>
        /// <param name="dir">文件夹相对路径</param>
        /// <returns></returns>
        public List <CloudFileInfoModel> CreateDirectory(string dir)
        {
            List <CloudFileInfoModel> list = new List <CloudFileInfoModel>();

            //所有传入的路径都进行一次转换
            dir = PathConverter.LocalPathToRemotePath(dir);
            try
            {
                foreach (ICloudDiskAPI api in _loadedCloudDiskApi)
                {
                    var oneDir = api.CreateDirectory(dir);
                    //在传出返回值之前将远程路径的形式替换成本地路径
                    oneDir.LocalPath = PathConverter.RemotePathToLocalPath(oneDir.Path);
                    list.Add(oneDir);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("同步文件夹时出错!" + ex.Message);
            }
            return(list);
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        /// <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);
        }