示例#1
0
        public void ReadProgressList()
        {
            // Read progress list
            List<ProgressInfo> progressList = JsonConvert.DeserializeObject<List<ProgressInfo>>(
                File.ReadAllText(GlobalHelper.ProgressList)).Select(c => (ProgressInfo)c).ToList();

            List<ProgressInfo> tmpProgressList = new List<ProgressInfo>();
            tmpProgressList.AddRange(progressList);

            if (progressList.Count > 0)
            {
                ApiHelper api = new ApiHelper();
                foreach (ProgressInfo info in progressList)
                {
                    // 如果本地端未完成檔案存在才顯示於進度清單中,否則就將該筆進度刪除
                    if (File.Exists(info.LocalFilePath) && api.CheckPath(info.RemoteFilePath))
                    {
                        if (info.Type.Equals("Download"))
                        {
                            long localFileSize = new FileInfo(info.LocalFilePath).Length;
                            int percentage = Convert.ToInt32(((double)localFileSize / (double)info.FileSize) * 100);
                            _dataDownloadFiles.Add(new FileProgressItem
                            {
                                Name = System.IO.Path.GetFileName(info.LocalFilePath).Replace(GlobalHelper.TempDownloadFileExt, String.Empty),
                                Progress = percentage,
                                FileId = info.FileId
                            });
                        }
                        else
                        {
                            long remoteFileSize = api.GetFileSize(info.RemoteFilePath);
                            int percentage = Convert.ToInt32(((double)remoteFileSize / (double)info.FileSize) * 100);
                            _dataUploadFiles.Add(new FileProgressItem
                            {
                                Name = System.IO.Path.GetFileName(info.LocalFilePath).Replace(GlobalHelper.TempUploadFileExt, String.Empty),
                                Progress = percentage,
                                FileId = info.FileId
                            });
                        }
                    }
                    else
                    {
                        int indexOfFile = tmpProgressList.IndexOf(info);
                        tmpProgressList.RemoveAt(indexOfFile);
                    }
                }

                // Serialize progress list to json format
                string jsonList = JsonConvert.SerializeObject(tmpProgressList, Formatting.Indented);

                // Overwrite progress list
                File.WriteAllText(GlobalHelper.ProgressList, jsonList, Encoding.UTF8);
            }
        }
示例#2
0
        public void UpdateProgressList(string type, string remoteFilePath, string localFilePath, string fileHash="")
        {
            // Create fileId
            string fileId = String.Format("{0}_{1}",
                type, Guid.NewGuid().ToString().Replace("-", String.Empty));

            // Get remote file size
            ApiHelper api = new ApiHelper();
            long fileSize = 0;
            if (type.Equals("Download"))
            {
                fileSize = api.GetFileSize(remoteFilePath);
            }
            else
            {
                fileSize = new FileInfo(localFilePath).Length;
            }

            // Prepare CFile Object
            CFile dbFile = new CFile();
            dbFile.FileHash = fileHash;
            dbFile.OriginFileName = remoteFilePath.Substring(remoteFilePath.LastIndexOf('/') + 1).Replace(GlobalHelper.TempUploadFileExt,"");

            // Read progress list
            List<ProgressInfo> progressList = JsonConvert.DeserializeObject<List<ProgressInfo>>(
                File.ReadAllText(GlobalHelper.ProgressList)).Select(c => (ProgressInfo)c).ToList();

            // Check file is duplicated
            if (type.Equals("Download"))
            {
                if (File.Exists(localFilePath) || File.Exists(localFilePath + GlobalHelper.TempDownloadFileExt))
                {
                    int i = 1;
                    string filePathWithoutExt = String.Format(@"{0}\{1}",
                        System.IO.Path.GetDirectoryName(localFilePath),
                        System.IO.Path.GetFileNameWithoutExtension(localFilePath));
                    string filePathExt = System.IO.Path.GetExtension(localFilePath);

                    while (true)
                    {
                        localFilePath = String.Format("{0} ({1}){2}",
                            filePathWithoutExt,
                            i,
                            filePathExt);
                        if (File.Exists(localFilePath) || File.Exists(localFilePath + GlobalHelper.TempDownloadFileExt))
                        {
                            i++;
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    localFilePath += GlobalHelper.TempDownloadFileExt;
                }
            }
            else
            {
                string[] splitPath = remoteFilePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                // 命名規則:公司名稱_產線編號_檔案分類編號_時間戳記.副檔名
                WFTPDbContext db = new WFTPDbContext();
                var info =
                    (from classify in db.Lv1Classifications
                    from customer in db.Lv2Customers
                    from branch in db.Lv3CustomerBranches
                    from line in db.Lv4Lines
                    from category in db.Lv5FileCategorys
                    where classify.ClassName == splitPath[0] &&
                          customer.CompanyName == splitPath[1] && customer.ClassifyId == classify.ClassifyId &&
                          branch.BranchName == splitPath[2] && branch.CompanyId == customer.CompanyId &&
                          line.LineName == splitPath[3] && line.BranchId == branch.BranchId &&
                          category.ClassName == splitPath[4]
                    select new
                    {
                        CompanyName = customer.CompanyName,
                        LineId = line.LineId,
                        CategoryId = category.FileCategoryId
                    }).First();

                remoteFilePath = String.Format("{0}/{1}_{2}_{3}_{4}{5}{6}",
                    remoteFilePath.Substring(0, remoteFilePath.LastIndexOf('/')),
                    info.CompanyName,
                    info.LineId.ToString(),
                    info.CategoryId.ToString(),
                    System.DateTime.Now.ToString("yyyyMMddHHmmssffff"),
                    System.IO.Path.GetExtension(remoteFilePath),
                    GlobalHelper.TempUploadFileExt);

                // Fill complete CFile value
                dbFile.FileCategoryId = info.CategoryId;
                dbFile.FileName = remoteFilePath.Substring(remoteFilePath.LastIndexOf('/') + 1).Replace(GlobalHelper.TempUploadFileExt, "");
                dbFile.LineId = info.LineId;
                dbFile.Path = remoteFilePath.Replace(GlobalHelper.TempUploadFileExt, "");

            }

            // Create new progress info
            ProgressInfo progressInfo = new ProgressInfo()
            {
                Type = type,
                RemoteFilePath = remoteFilePath,
                LocalFilePath = localFilePath,
                FileSize = fileSize,
                FileId = fileId
            };

            progressList.Add(progressInfo);

            // Serialize progress list to json format
            string jsonList = JsonConvert.SerializeObject(progressList, Formatting.Indented);

            // Overwrite progress list
            File.WriteAllText(GlobalHelper.ProgressList, jsonList, Encoding.UTF8);

            if (type.Equals("Download"))
            {
                // Add file to download list
                Switcher.progress._dataDownloadFiles.Add(new FileProgressItem
                {
                    Name = System.IO.Path.GetFileName(localFilePath).Replace(GlobalHelper.TempDownloadFileExt, String.Empty),
                    Progress = 0,
                    FileId = fileId
                });

                // Download file from FTP server
                Dictionary<string, string> fileInfo = new Dictionary<string, string>();
                fileInfo.Add("FileId", fileId);
                fileInfo.Add("RemoteFilePath", remoteFilePath);
                fileInfo.Add("LocalFilePath", System.IO.Path.GetDirectoryName(localFilePath));
                fileInfo.Add("LocalFileName", System.IO.Path.GetFileName(localFilePath));
                fileInfo.Add("RemoteFileSize", Convert.ToString(fileSize));

                StartDownload(fileInfo);
            }
            else
            {
                // Add file to upload list
                Switcher.progress._dataUploadFiles.Add(new FileProgressItem
                {
                    Name = System.IO.Path.GetFileName(localFilePath).Replace(GlobalHelper.TempUploadFileExt, String.Empty),
                    Progress = 0,
                    FileId = fileId
                });

                // Upload file to FTP server
                Dictionary<string, string> fileInfo = new Dictionary<string, string>();
                fileInfo.Add("FileId", fileId);
                fileInfo.Add("RemoteFilePath", remoteFilePath);
                fileInfo.Add("LocalFilePath", localFilePath);
                //fileInfo.Add("LocalFileName", System.IO.Path.GetFileName(localFilePath));
                fileInfo.Add("LocalFileSize", Convert.ToString(fileSize));
                fileInfo.Add("ModelCreateUser", dbFile.CreateUser);
                fileInfo.Add("ModelFileCategoryId", dbFile.FileCategoryId.ToString());
                fileInfo.Add("ModelFileHash", dbFile.FileHash);
                fileInfo.Add("ModelFileName", dbFile.FileName);
                fileInfo.Add("ModelLastEditUser", dbFile.LastEditUser);
                fileInfo.Add("ModelLineId", dbFile.LineId.ToString());
                fileInfo.Add("ModelOriginFileName", dbFile.OriginFileName);
                fileInfo.Add("ModelPath", dbFile.Path);

                StartUpload(fileInfo);
            }
        }
示例#3
0
        public void bgworkerStartUpload_DoWorkHandler(object sender, DoWorkEventArgs e)
        {
            ApiHelper api = new ApiHelper();
            BackgroundWorker bgworkerStartUpload = sender as BackgroundWorker;
            Dictionary<string, string> fileInfo = (Dictionary<string, string>)e.Argument;
            Dictionary<string, string> asyncResult = new Dictionary<string, string>();
            asyncResult.Add("IsCompleted", "false");
            asyncResult.Add("FileId", fileInfo["FileId"]);
            asyncResult.Add("RemoteFilePath", fileInfo["RemoteFilePath"]);

            // Asynchronous FTP Upload
            Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

            bool success;

            success = ftp.UnlockComponent(GlobalHelper.ComponentCode);
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            ftp.Hostname = GlobalHelper.FtpHost;
            ftp.Username = GlobalHelper.FtpUsername;
            ftp.Password = GlobalHelper.FtpPasswrod;
            // Resume upload
            ftp.RestartNext = true;

            // Connect and login to the FTP server.
            success = ftp.Connect();
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            string localFilename = fileInfo["LocalFilePath"];
            string remoteFilename = fileInfo["RemoteFilePath"];
            long localFilesize = Convert.ToInt64(fileInfo["LocalFileSize"]);

            success = ftp.AsyncPutFileStart(localFilename, remoteFilename);
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            while (ftp.AsyncFinished != true)
            {
                if (_cancelList.Contains(fileInfo["FileId"]))
                {
                    ftp.AsyncAbort();
                    break;
                }

                if (api.CheckPath(remoteFilename))
                {
                    long remoteFilesize = api.GetFileSize(remoteFilename);
                    double percentage = ((double)remoteFilesize / (double)localFilesize) * 100;
                    bgworkerStartUpload.ReportProgress((int)percentage);
                }

                // Sleep 0.5 second.
                ftp.SleepMs(500);
            }

            bool uploadSuccess = false;
            // Did the upload succeed?
            if (ftp.AsyncSuccess == true)
            {
                uploadSuccess = true;
                bgworkerStartUpload.ReportProgress(100);
                asyncResult["IsCompleted"] = "true";
            }
            else
            {

            }

            ftp.Disconnect();
            ftp.Dispose();

            // Change Local file name back to original
            string originalFilePath = fileInfo["LocalFilePath"].Replace(GlobalHelper.TempUploadFileExt, String.Empty);
            if (uploadSuccess || _cancelList.Contains(fileInfo["FileId"]))
            {
                File.Move(fileInfo["LocalFilePath"], originalFilePath);
            }

            if (uploadSuccess)
            {
                // Move local file to Recycle bin
                FileSystem.DeleteFile(originalFilePath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                // Remove temp extension from remote file
                string newName = remoteFilename.Replace(GlobalHelper.TempUploadFileExt, String.Empty);
                api.Rename(remoteFilename, newName);
                // Insert record into db
                int categoryId = Convert.ToInt32(fileInfo["ModelFileCategoryId"]);
                int lineId = Convert.ToInt32(fileInfo["ModelLineId"]);
                CFile.InsertOrUpdate(null, categoryId, lineId, fileInfo["ModelOriginFileName"],
                                           fileInfo["ModelFileName"], false, GlobalHelper.LoginUserID, fileInfo["ModelFileHash"]);
            }

            e.Result = asyncResult;
        }