Example #1
0
        public CloudFileInfoModel CreateDirectory(string dir)
        {
            SortedDictionary <string, string> ParamList = getParamList();

            ParamList.Add("root", "app_folder");
            ParamList.Add("path", dir);
            string SourceString = GetApiSourceString(this.createFolderUrl, ParamList);
            string SecretKey    = GetSecretKey();
            string Sign         = GetSignature(SourceString, SecretKey);

            ParamList.Add("oauth_signature", Sign);
            string             url        = this.createFolderUrl + "?" + ParamToUrl(ParamList, false);
            object             jsonAccess = GetGeneralContent(url);
            XmlNode            node       = JsonHelper.DeserializeToXmlNode(jsonAccess.ToString());
            CloudFileInfoModel fileInfo   = new CloudFileInfoModel();

            fileInfo.Path = PathConverter.RemotePathToLocalPath(Convert.ToString(node.ChildNodes[0].SelectSingleNode("path").InnerText));
            return(fileInfo);
        }
Example #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);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
0
        public CloudFileInfoModel UploadFile(byte[] fileContent, string filePath)
        {
            NameValueCollection stringDict = new NameValueCollection();

            stringDict.Add("file", "fileKeyValue");
            object  jsonAccess = GetGeneralContent(this.fileUploadLocationUrl);
            XmlNode node       = JsonHelper.DeserializeToXmlNode(jsonAccess.ToString());
            String  updateUrl  = Convert.ToString(node.ChildNodes[0].SelectSingleNode("url").InnerText);
            String  status     = Convert.ToString(node.ChildNodes[0].SelectSingleNode("stat").InnerText);

            if (status.Equals("OK"))
            {
                String Url = string.Format(this.fileUploadUrl, updateUrl);
                SortedDictionary <string, string> ParamList = getParamList();
                ParamList.Add("root", "app_folder");
                ParamList.Add("path", filePath);
                ParamList.Add("overwrite", "true");
                StringBuilder sb = new StringBuilder();
                sb.Append("POST&");
                sb.Append(UrlEncode(Url) + "&");
                string InnerParam = ParamToUrl(ParamList, true);

                sb.Append(UrlEncode(InnerParam));
                string SourceString = sb.ToString();

                string SecretKey = GetSecretKey();
                string Sign      = GetSignature(SourceString, SecretKey);
                ParamList.Add("oauth_signature", Sign);
                String finalUrl = Url + "?" + ParamToUrl(ParamList, false);

                string responseContent;
                var    memStream  = new MemoryStream();
                var    webRequest = (HttpWebRequest)WebRequest.Create(finalUrl);
                // 边界符
                var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
                // 边界符
                var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
                // 最后的结束符
                var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");

                // 设置属性
                webRequest.Method      = "POST";
                webRequest.Timeout     = 10000;
                webRequest.ContentType = "multipart/form-data; boundary=" + boundary;

                // 写入文件
                const string filePartHeader =
                    "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                    "Content-Type: application/octet-stream\r\n\r\n";
                var header      = string.Format(filePartHeader, "file", filePath);
                var headerbytes = Encoding.UTF8.GetBytes(header);

                memStream.Write(beginBoundary, 0, beginBoundary.Length);
                memStream.Write(headerbytes, 0, headerbytes.Length);


                memStream.Write(fileContent, 0, fileContent.Length);


                // 写入字符串的Key
                var stringKeyHeader = "\r\n--" + boundary +
                                      "\r\nContent-Disposition: form-data; name=\"{0}\"" +
                                      "\r\n\r\n{1}\r\n";

                foreach (byte[] formitembytes in from string key in stringDict.Keys
                         select string.Format(stringKeyHeader, key, stringDict[key])
                         into formitem
                         select Encoding.UTF8.GetBytes(formitem))
                {
                    memStream.Write(formitembytes, 0, formitembytes.Length);
                }



                // 写入最后的结束边界符
                memStream.Write(endBoundary, 0, endBoundary.Length);

                webRequest.ContentLength = memStream.Length;

                var requestStream = webRequest.GetRequestStream();

                memStream.Position = 0;
                var tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();

                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                requestStream.Close();

                var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();

                using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
                                                               Encoding.GetEncoding("utf-8")))
                {
                    responseContent = httpStreamReader.ReadToEnd();
                }

                httpWebResponse.Close();
                webRequest.Abort();
                node = JsonHelper.DeserializeToXmlNode(responseContent.ToString());
                CloudFileInfoModel fileInfo = new CloudFileInfoModel();
                fileInfo.Size = Convert.ToString(node.ChildNodes[0].SelectSingleNode("size").InnerText);
                fileInfo.Path = PathConverter.RemotePathToLocalPath(filePath);
                return(fileInfo);
            }
            return(null);
        }