Example #1
0
        /// <summary>
        /// 上傳 Ios Android 安裝檔案
        /// </summary>
        /// <param name="appOsInfo"></param>
        /// <param name="uploadFile"></param>
        /// <returns></returns>
        public async Task <TResult <AppOsInfo> > UploadInstallFile(AppOsInfo appOsInfo, IMatFileUploadEntry uploadFile)
        {
            //存放多載點 plist 檔案名稱
            List <DownloadUrlList> downloadUrlList = new List <DownloadUrlList>();

            #region 驗證檔案格式

            int findExtension = uploadFile.Name.LastIndexOf(".");

            if (string.IsNullOrEmpty(appOsInfo.Version))
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_VERSIONREQUIRED, "必須輸入 版本號碼"));
            }

            if (appOsInfo.Photo.Where(p => p.FileNumber == 5).FirstOrDefault() == null)
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_OSFORMATTYPE, "必須上傳 Icon 圖片"));
            }

            if (findExtension == -1)
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_OSFORMATTYPE, "檔案格式錯誤: 無附檔名"));
            }

            string extension = uploadFile.Name.Substring(uploadFile.Name.LastIndexOf(".")).ToLower();

            //驗證 Ios 是否符合格式 目前只看.ipa 檔案
            if (appOsInfo.OSType == OsType.Ios && !extension.Equals(".ipa"))
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_OSFORMATTYPE, "檔案格式限制:  IOS 安裝檔應為 .ipa 格式"));
            }

            //驗證 Android 是否符合格式 目前只看.apk 檔案
            if (appOsInfo.OSType == OsType.Android && !extension.Equals(".apk"))
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_OSFORMATTYPE, "檔案格式限制:  Android 安裝檔應為 .apk 格式"));
            }

            #endregion

            string fileName = $"{appOsInfo.Version}-{Guid.NewGuid().ToString("N")}{extension}"; //Create a new Name
            string osPath   = OsType.Ios == appOsInfo.OSType ? "IOS" : "Android";
            string filePath = Path.Combine(_configContext.UploadPath, appOsInfo.AppNameEn.Replace(" ", ""), osPath, "Install");

            //檔案資料夾路徑不存在就新增
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
            {
                await uploadFile.WriteToStreamAsync(fs);
            }

            #region 如果是 IOS 必須做出 Plist 檔案 和多載點 plist

            //如果是IOS 除了上傳.ipa 檔案 還要上傳 Plist xml file
            if (appOsInfo.OSType == OsType.Ios)
            {
                //圖片 url 路徑
                AppPhotoContent iosIconPhoto = appOsInfo.Photo.Where(P => P.FileNumber == 5).FirstOrDefault();

                //多載點 plist
                foreach (OtherSideEndPoint endPoint in _configContext.OtherSideEndPoint)
                {
                    // 組plist 黨案名稱
                    string pListFileName = $"{Guid.NewGuid().ToString("N")}{".plist"}";

                    // 組 ipa url 路徑
                    string ipaPath = $"{endPoint.Url}/{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{fileName}";

                    // 組圖片 url 路徑
                    string iconPath = iosIconPhoto != null ? $"{endPoint.Url}/{iosIconPhoto.FilePath.Replace(" ", "")}" : "";

                    //做出 plist xml 內容物
                    string plistXml = UploadFileHepler.GetPlistXml(ipaPath, iconPath, appOsInfo.PackageName, appOsInfo.Version, appOsInfo.AppNameEn);

                    //存入 Plist 實體路徑
                    File.WriteAllText(Path.Combine(filePath, pListFileName), plistXml);

                    //原始路徑 存在Wis_AppOS table
                    if (endPoint.Site == "WHQ")
                    { //原始載點不加入 DownloadUrlList
                      //為 plist 檔案路徑
                        appOsInfo.FilePath = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{pListFileName}";

                        //為 .ipa 檔案路徑
                        appOsInfo.FileName = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{fileName}";
                    }
                    else
                    {//多載點資訊 存在 Wis_AppOs_Other
                        downloadUrlList.Add(new DownloadUrlList
                        {
                            Site = endPoint.Site,
                            Url  = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{pListFileName}"//plist 路徑
                        });
                    }
                }

                //前端必須按下儲存按鈕  url資訊才會寫入 DB
                //Ios 上傳檔案路徑 是 plist檔案路徑
                appOsInfo.DownloadUrlList = downloadUrlList;
            }

            #endregion

            #region Android 處理檔案 路徑

            //Android 直接存放 apk 檔案路徑
            if (appOsInfo.OSType == OsType.Android)
            {
                foreach (OtherSideEndPoint endPoint in _configContext.OtherSideEndPoint)
                {
                    //Wis_AppOS url 路徑處理
                    if (endPoint.Site == "WHQ")
                    {
                        //為 .apk 檔案路徑
                        appOsInfo.FilePath = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{fileName}";

                        //為 .apk 原檔案名稱
                        appOsInfo.FileName = uploadFile.Name;
                    }
                    else
                    {//Wis_AppOs_Other 資訊
                        downloadUrlList.Add(new DownloadUrlList
                        {
                            Site = endPoint.Site,
                            Url  = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Install"}/{fileName}"//.apk 檔案路徑
                        });
                    }
                }
                //Android 上傳檔案路徑 是apk檔案路徑 與原本路徑一模一樣
                appOsInfo.DownloadUrlList = downloadUrlList;
            }
            #endregion

            return(TResult <AppOsInfo> .OK(appOsInfo, "OK"));
        }
Example #2
0
        /// <summary>
        /// 上傳圖片檔案 對象物件 AppOsInfo 裡的 AppPhotoContent
        /// </summary>
        /// <param name="fileNumber">記錄圖檔上傳位置 5 為安裝檔案Icon圖片</param>
        /// <param name="osType">IOS = 1 Android=2</param>
        /// <param name="appIos"></param>
        /// <param name="uploadFile"></param>
        /// <returns>實體路徑圖片</returns>
        public async Task <TResult <AppOsInfo> > UploadImageFile(int fileNumber, AppOsInfo appOsInfo, IMatFileUploadEntry uploadFile)
        {
            #region 驗證圖片檔案格式

            //驗證圖片是否符合格式  目前只接受 PNG JPEG
            ImageFormat imageType = await _uploadFileService.CheckImageType(uploadFile);

            if (imageType == null)
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_IMAGEFORMATTYPE, "檔案格式限制: jpg、jpeg、png"));
            }

            int findExtension = uploadFile.Name.LastIndexOf(".");

            if (findExtension == -1)
            {
                return(TResult <AppOsInfo> .Fail(null, FaultInfoRcConstants.ERR_CODE_OSFORMATTYPE, "檔案格式錯誤: 無附檔名"));
            }

            #endregion

            string extension = uploadFile.Name.Substring(uploadFile.Name.LastIndexOf(".")).ToLower();
            string fileName  = $"{Guid.NewGuid().ToString("N")}{extension}"; //Create a new Name
            string osPath    = OsType.Ios == appOsInfo.OSType ? "IOS" : "Android";
            string filePath  = Path.Combine(_configContext.UploadPath, appOsInfo.AppNameEn.Replace(" ", ""), osPath, "Images");

            //檔案資料夾路徑不存在就新增
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
            {
                await uploadFile.WriteToStreamAsync(fs);
            }

            AppPhotoContent appPhotoContent = new AppPhotoContent
            {
                AppID      = appOsInfo.AppID,
                AppOS      = appOsInfo.OSType,
                FileNumber = fileNumber,
                FileName   = fileName,
                //exapmle Wistron%20Ark\IOS\Images\5-623cefb49e3946a28198464dd72b5bb4.png 子目錄位址
                FilePath  = $"{appOsInfo.AppNameEn.Replace(" ", "")}/{osPath}/{"Images"}/{fileName}",
                PhotoType = fileNumber == 5 ? PhotoType.IconType : PhotoType.NormalType
            };

            //把有存在編號的 去除 在Add更新過後的 但還未更新資料庫 等待 按下儲存按鈕
            AppPhotoContent existed = appOsInfo.Photo.Where(p => p.FileNumber == fileNumber).FirstOrDefault();
            if (existed != null)
            {
                appOsInfo.Photo.Remove(existed);
                appOsInfo.Photo.Add(appPhotoContent);
            }
            else
            {
                appOsInfo.Photo.Add(appPhotoContent);
            }

            return(TResult <AppOsInfo> .OK(appOsInfo, "OK"));
        }