Example #1
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);
            }
        }
Example #2
0
        public static void InsertOrUpdate(int? fileId,int categoryId, int lineId, string originFileName, string fileName, bool? isDelete, string loginUserID, string fileHash)
        {
            WFTPDbContext db = new WFTPDbContext();
            if (fileId == null) //Insert
            {
                try
                {
                    CFile f = new CFile();
                    f.FileCategoryId = categoryId;
                    f.LineId = lineId;
                    f.OriginFileName = originFileName;
                    f.FileName = fileName;
                    f.FileHash = fileHash;
                    f.IsDeleted = false;
                    f.CreateDate = DateTime.Now;
                    f.LastUploadDate = DateTime.Now;
                    f.LastEditUser = loginUserID;
                    f.CreateUser = loginUserID;
                    db.Lv6Files.InsertOnSubmit(f);
                    db.SubmitChanges();
                    f.Path = DBHelper.GenerateFileFullPath(f.FileId);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else //Update
            {
                try
                {
                    var file = (from files in db.GetTable<CFile>()
                                where files.FileId == fileId
                                select files).SingleOrDefault();
                    if (categoryId > 0)
                        file.FileCategoryId = categoryId;

                    if (!String.IsNullOrEmpty(fileName))
                        file.FileName = fileName;

                    if (isDelete.HasValue)
                        file.IsDeleted = (bool)isDelete;

                    file.LastEditUser = loginUserID;
                    file.LastUploadDate = DateTime.Now;

                    if(lineId > 0)
                        file.LineId = lineId;

                    if (!String.IsNullOrEmpty(originFileName))
                        file.OriginFileName = originFileName;

                    file.Path = DBHelper.GenerateFileFullPath(file.FileId);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }