Exemplo n.º 1
0
        /// <summary>
        /// Gets the amazon upload data.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="selectedFileName">Name of the selected file.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="recordId">The record identifier.</param>
        /// <returns></returns>
        private AppFileAmazonS3DirectHttpUploadData GetAmazonUploadData(AppFileType type, vAppFile file)
        {
            Check.Require(file != null);
            Check.Require(type != null);

            var encoding = new System.Text.UTF8Encoding();

            AppFileAmazonS3DirectHttpUploadData result = new AppFileAmazonS3DirectHttpUploadData();

            string uuid = file.FileUUID.ToString("N");

            //result.AppFileID = file.AppFileID;
            result.UploadUrl   = GetFileServerHttp();
            result.AccessKeyID = FWUtils.ConfigUtils.GetAppSettings().AmazonCloud.S3.AccessKeyID;
            result.Key         = GetFileKeyByFileInfo(file);
            result.Acl         = s3Settings.DefaultUploadAcl;
            string policyText = GetAmazonPolicyText(type, result.Key, result.Acl, uuid);

            result.PolicyBase64    = Convert.ToBase64String(encoding.GetBytes(policyText));
            result.SignatureBase64 = GetAmazonSignatureBase64(policyText);

            result.ServerSideEncryption = s3Settings.DefaultServerSideEncryption;
            result.AppFileUUID          = uuid;
            result.AppFileID            = file.AppFileID;

            return(result);
        }
Exemplo n.º 2
0
        private void TryHandleVsListMatch(AppFileType FileType)
        {
            // 自动匹配数据归零
            M_Auto_Begin = int.MinValue;
            M_Auto_End   = null;

            var FileList = GetFileListByVsList(FileType);

            foreach (var file in FileList)
            {
                string matchKey = GetMatchKeyByFileName(file.Name, FileType, FileList);

                var findVsItem = (matchKey != null) ? VsList.Find(o => o.MatchKey == matchKey) : null; // By matchKey

                if (findVsItem == null)
                {
                    findVsItem = VsList.Find(o =>
                    {
                        if (FileType == AppFileType.Video)
                        {
                            return(o.Video == file.FullName);
                        }
                        if (FileType == AppFileType.Sub)
                        {
                            return(o.Sub == file.FullName);
                        }
                        return(false);
                    });  // 通过文件名查找到的现成的 VsItem
                }
                // 仅更新数据
                if (findVsItem != null)
                {
                    if (FileType == AppFileType.Video)
                    {
                        findVsItem.Video  = file.FullName;
                        findVsItem.Status = VsStatus.SubLack;
                        VsList.RemoveAll(o => o != findVsItem && o.Video == findVsItem.Video); // 删除其他同类项目
                    }
                    else if (FileType == AppFileType.Sub)
                    {
                        findVsItem.Sub    = file.FullName;
                        findVsItem.Status = VsStatus.VideoLack;
                        VsList.RemoveAll(o => o != findVsItem && o.Sub == findVsItem.Sub); // 删除其他同类项目
                    }

                    if (findVsItem.Video != null && findVsItem.Sub != null)
                    {
                        findVsItem.Status = VsStatus.Ready;
                    }

                    findVsItem.MatchKey = matchKey;
                    if (string.IsNullOrWhiteSpace(findVsItem.MatchKey))
                    {
                        findVsItem.Status = VsStatus.Unmatched;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void AmazonUploadRequest(AppFileType type, AppFileAmazonUploadRequestSP p)
        {
            if (type == null)
            {
                throw new Exception("File type is not specified AmazonUploadRequest.");
            }

            // checking file size
            if (string.IsNullOrEmpty(p.FileSize) == false)
            {
                int size = 0;
                if (int.TryParse(p.FileSize, out size))
                {
                    if (size < type.MinFileSize || size > type.MaxFileSize)
                    {
                        throw new BRException(
                                  string.Format(BusinessErrorStrings.AppFile.InvalidFileSize_MinMax,
                                                FWUtils.MiscUtils.GetReadableFileSize(type.MinFileSize),
                                                FWUtils.MiscUtils.GetReadableFileSize(type.MaxFileSize)
                                                ));
                    }
                }
            }

            // checking file type
            string fileType = FWUtils.MiscUtils.GetFileTypeByFileName(p.FileName);

            if (string.IsNullOrEmpty(p.FileName) == false &&
                string.IsNullOrEmpty(type.AcceptableFormatsCommaSeparated) == false)
            {
                if (string.IsNullOrEmpty(fileType))
                {
                    throw new BRException(BusinessErrorStrings.AppFile.FileTypeEmpty);
                }

                string[] acceptableFileTypeArray = type.AcceptableFormatsCommaSeparated.Split(',');
                bool     isAcceptable            = false;
                foreach (string s in acceptableFileTypeArray)
                {
                    if (s.ToLower() == fileType)
                    {
                        isAcceptable = true;
                        break;
                    }
                }
                if (isAcceptable == false)
                {
                    throw new BRException(string.Format(BusinessErrorStrings.AppFile.InvalidFileType,
                                                        fileType,
                                                        type.AcceptableFormatsCommaSeparated
                                                        ));
                }
            }
        }
Exemplo n.º 4
0
        // protected void MatchVideoSub() => Task.Factory.StartNew(() => _MatchVideoSub());

        private List <FileInfo> GetFileListByVsList(AppFileType FileType)
        {
            if (FileType == AppFileType.Video)
            {
                return(new List <FileInfo>(VsList.Where(o => o.Video != null).Select(o => o.VideoFileInfo)));
            }
            if (FileType == AppFileType.Sub)
            {
                return(new List <FileInfo>(VsList.Where(o => o.Sub != null).Select(o => o.SubFileInfo)));
            }
            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// returns policy text for a selected filetype
        /// </summary>
        /// <param name="fileType"></param>
        /// <returns></returns>
        private string GetAmazonPolicyText(AppFileType fileType, string key, string acl, string uuid)
        {
            const string AWS_ISO_FORMAT       = "yyyy-MM-ddTHH:mm:ss.fffZ";
            string       bucketName           = s3Settings.BucketName;
            string       expirationDate       = DateTime.Now.AddHours(10).ToUniversalTime().ToString(AWS_ISO_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
            string       serversideEncryption = s3Settings.DefaultServerSideEncryption;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("{ \"expiration\": \"").Append(expirationDate).Append("\",");
            sb.Append("  \"conditions\": [");
            sb.Append("    {\"bucket\": \"").Append(bucketName).Append("\"},");
            sb.Append("    [\"starts-with\", \"$key\", \"").Append(key).Append("\"],");
            sb.Append("    {\"acl\": \"").Append(acl).Append("\"},");
            sb.Append("    [\"content-length-range\", \"").Append(fileType.MinFileSize).Append("\", \"").Append(fileType.MaxFileSize).Append("\"],");
            sb.Append("    {\"success_action_status\": \"").Append(200).Append("\"},");
            //sb.Append("    {\"success_action_redirect\": \"").Append(successRedirectUrl).Append("\"},");
            //sb.Append("    [\"starts-with\", \"$Content-Type\", \"image/\"],");
            sb.Append("    {\"x-amz-meta-uuid\": \"").Append(uuid).Append("\"},");
            // we shouldn't have redirect here because it's a UI thing. We may add redirect later as a parameter coming from UI.
            //sb.Append("    {\"redirect\": \"").Append(FWUtils.ConfigUtils.GetAppSettings().Project.WebsiteUrl).Append("Scripts/jquery-fileupload/cors/result.html?a=1").Append("\"},");
            if (serversideEncryption != "None") // having None as a value will cause an exception in Amazon Post
            {
                sb.Append("    {\"x-amz-server-side-encryption\": \"").Append(s3Settings.DefaultServerSideEncryption).Append("\"},");
            }
            sb.Append("  ]");
            sb.Append("}");


            //var policy =
            //new
            //{
            //    // one hour policy
            //    expiration = DateTime.Now.AddHours(10).ToUniversalTime().ToString(AWS_ISO_FORMAT, System.Globalization.CultureInfo.InvariantCulture),
            //    conditions = new object[]
            //            {
            //                new { bucket = bucketName },
            //                new [] { "starts-with", "$key", key },
            //                new { acl = acl },
            //                new { success_action_status = "200" },
            //                new object[] { "content-length-range", fileType.MinFileSize, fileType.MaxFileSize },
            //                //new [] { "starts-with", "$Content-Type", "" }
            //                new [] { "x-amz-meta-uuid", uuid},
            //                new [] { "x-amz-server-side-encryption", s3Settings.DefaultServerSideEncryption}
            //            }
            //};

            //var serializedPolicy = FWUtils.EntityUtils.JsonSerializeObject(policy);
            //String serializedPolicy = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(policy);
            String serializedPolicy = sb.ToString();

            return(serializedPolicy);
        }
Exemplo n.º 6
0
        private void TestStrRematch(AppFileType FileType, bool displayAlert = true)
        {
            Regex regex = GetRegexInstance(FileType, displayAlert);

            if (FileType == AppFileType.Video)
            {
                V_TestResult.Text = MainForm.GetMatchKeyByRegex(V_TestStr.Text.Trim(), regex);
            }
            else if (FileType == AppFileType.Sub)
            {
                S_TestResult.Text = MainForm.GetMatchKeyByRegex(S_TestStr.Text.Trim(), regex);
            }
        }
Exemplo n.º 7
0
        // 获取匹配字符
        private string GetMatchKeyByFileName(string fileName, AppFileType fileType, List <FileInfo> FileList)
        {
            string matchKey = null;

            if (CurtMatchMode == MatchMode.Auto)
            {
                if (M_Auto_Begin == int.MinValue)
                {
                    M_Auto_Begin = GetEpisodePosByList(FileList);                               // 视频文件名集数开始位置
                }
                if (M_Auto_End == null)
                {
                    M_Auto_End = GetEndStrByList(FileList, M_Auto_Begin);
                }
                if (M_Auto_Begin > -1 && M_Auto_End != null)
                {
                    matchKey = GetEpisByFileName(fileName, M_Auto_Begin, M_Auto_End); // 匹配字符
                }
            }
            else if (CurtMatchMode == MatchMode.Manu)
            {
                if (fileType == AppFileType.Video)
                {
                    matchKey = GetMatchKeyByBeginEndStr(fileName, M_Manu_V_Begin, M_Manu_V_End);
                }
                else if (fileType == AppFileType.Sub)
                {
                    matchKey = GetMatchKeyByBeginEndStr(fileName, M_Manu_S_Begin, M_Manu_S_End);
                }
            }
            else if (CurtMatchMode == MatchMode.Regex)
            {
                if (fileType == AppFileType.Video && M_Regx_V != null)
                {
                    matchKey = GetMatchKeyByRegex(fileName, M_Regx_V);
                }
                else if (fileType == AppFileType.Sub && M_Regx_S != null)
                {
                    matchKey = GetMatchKeyByRegex(fileName, M_Regx_S);
                }
            }

            if (string.IsNullOrWhiteSpace(matchKey))
            {
                matchKey = null;
            }
            return(matchKey);
        }
Exemplo n.º 8
0
        private void DiscardListSelectedItemsFile(AppFileType FileType)
        {
            if (FileListUi.SelectedItems.Count <= 0)
            {
                return;
            }

            string label = "";

            if (FileType.Equals(AppFileType.Video))
            {
                label = "视频";
            }
            else if (FileType.Equals(AppFileType.Sub))
            {
                label = "字幕";
            }

            if (AppSettings.ListItemRemovePrompt)
            {
                var result = MessageBox.Show($"你要丢弃选定项目的{label}吗?源文件不会被删除", $"丢弃所选项的{label}", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                    return;
                }
            }

            foreach (ListViewItem item in FileListUi.SelectedItems)
            {
                if (item.Tag == null)
                {
                    item.Remove();
                    continue;
                }

                var vsItem = (VsItem)item.Tag;
                if (FileType.Equals(AppFileType.Video))
                {
                    vsItem.Video = null;
                }
                if (FileType.Equals(AppFileType.Sub))
                {
                    vsItem.Sub = null;
                }
            }

            RefreshFileListUi();
        }
Exemplo n.º 9
0
        private void MatchRuleUpdated(AppFileType FileType)
        {
            if (FileType == AppFileType.Video)
            {
                V_Begin        = null;
                V_End          = null;
                V_Matched.Text = "未匹配";
                var tpl = V_Tpl.Text.Trim();

                if (string.IsNullOrWhiteSpace(tpl))
                {
                    return;
                }
                var pos = tpl.ToUpper().IndexOf(MatchSign);
                if (pos <= -1)
                {
                    return;
                }
                var afterPos = pos + MatchSign.Length;

                V_Begin        = tpl.Substring(0, pos);
                V_End          = tpl.Substring(afterPos, tpl.Length - afterPos);
                V_Matched.Text = "匹配结果: " + MainForm.GetMatchKeyByBeginEndStr(V_Raw, V_Begin, V_End);
            }
            else if (FileType == AppFileType.Sub)
            {
                S_Begin        = null;
                S_End          = null;
                S_Matched.Text = "未匹配";
                var tpl = S_Tpl.Text.Trim();

                if (string.IsNullOrWhiteSpace(tpl))
                {
                    return;
                }
                var pos = tpl.ToUpper().IndexOf(MatchSign);
                if (pos <= -1)
                {
                    return;
                }
                var afterPos = pos + MatchSign.Length;

                S_Begin        = tpl.Substring(0, pos);
                S_End          = tpl.Substring(afterPos, tpl.Length - afterPos);
                S_Matched.Text = "匹配结果: " + MainForm.GetMatchKeyByBeginEndStr(S_Raw, S_Begin, S_End);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Amazons the upload request information of for Amazon S3
        /// </summary>
        /// <param name="p">parameters</param>
        /// <returns></returns>
        public AppFileAmazonS3DirectHttpUploadData AmazonUploadRequest(AppFileAmazonUploadRequestSP p)
        {
            Check.Require(string.IsNullOrEmpty(p.FileName) == false);
            Check.Require(p.AppFileTypeID > 0);
            Check.Require(p.AppEntityRecordIDValue > 0);

            var biz = (AppFileBR)BusinessLogicObject;

            biz.AmazonUploadRequestPre(p);

            vAppFile    appFile  = null;
            AppFileType fileType = (AppFileType)AppFileTypeEN.GetService().GetByID(p.AppFileTypeID, new GetByIDParameters());

            // checking business rules at first
            biz.AmazonUploadRequest(fileType, p);

            if (fileType.HasSecurityCheck == true)
            {
                CheckUploadRequestSecurity((EntityEnums.AppEntityFileTypeEnum)fileType.AppFileTypeID, p);
            }

            if (fileType.MaxNumberOfFiles == 1) // single file upload
            {
                FilterExpression filter = new FilterExpression();
                filter.AddFilter(vAppFile.ColumnNames.AppFileTypeID, p.AppFileTypeID);
                filter.AddFilter(vAppFile.ColumnNames.AppEntityRecordIDValue, p.AppEntityRecordIDValue);
                IList <vAppFile> list = GetByFilterV(new GetByFilterParameters(filter));
                if (list.Count == 0) // create a record
                {
                    appFile = InsertNewFile(p.FileName, p.AppFileTypeID, p.AppEntityRecordIDValue);
                }
                else
                {
                    appFile = list[0];
                }
            }
            else  // multiple file upload
            {
                appFile = InsertNewFile(p.FileName, p.AppFileTypeID, p.AppEntityRecordIDValue);
            }

            long userId = FWUtils.SecurityUtils.GetCurrentUserIDLong();
            var  result = GetAmazonUploadData(fileType, appFile);

            return(result);
        }
Exemplo n.º 11
0
        private Regex GetRegexInstance(AppFileType FileType, bool displayAlert = true)
        {
            string regxStr = "";

            if (FileType == AppFileType.Video)
            {
                regxStr = VideoRegex.Text;
            }
            else if (FileType == AppFileType.Sub)
            {
                regxStr = SubRegex.Text;
            }

            if (string.IsNullOrWhiteSpace(regxStr))
            {
                return(null);
            }

            Regex regex = null;

            try
            {
                regex = new Regex(regxStr.Trim());
            }
            catch (Exception ex)
            {
                if (displayAlert)
                {
                    var label = "";
                    if (FileType == AppFileType.Video)
                    {
                        label = "视频";
                    }
                    else if (FileType == AppFileType.Sub)
                    {
                        label = "字幕";
                    }
                    MessageBox.Show($"{label} 正则语法有误 {ex.Message}{Environment.NewLine}{ex.StackTrace}", "正则语法错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return(regex);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Sends a request to replace a file in Amazon S3
        /// </summary>
        /// <param name="p">parameters</param>
        /// <returns></returns>
        public AppFileAmazonS3DirectHttpUploadData S3RequestReplaceFile(AppFileS3RequestReplaceFileSP p)
        {
            var file = GetByIDV(p.AppFileID);

            if (file != null)
            {
                //string key = GetFileKeyByFileInfo(file);
                //DeleteFromS3(key); // deletes the file from S3 to make sure that the old file doesn't exists

                AppFileType fileType = (AppFileType)AppFileTypeEN.GetService().GetByID(file.AppFileTypeID, new GetByIDParameters());
                long        userId   = FWUtils.SecurityUtils.GetCurrentUserIDLong();
                var         result   = GetAmazonUploadData(fileType, file);
                result.AppFileID = file.AppFileID;
                return(result);
            }
            else
            {
                throw new UserException(StringMsgs.BusinessErrors.RecordIsNotAvailable);
            }
        }
Exemplo n.º 13
0
        public static string GetAppFilePath(AppFileType fileType)
        {
            string path = string.Empty;

            switch (fileType)
            {
            case AppFileType.ImportExtractor:
                path = @"{0}\{1}\last_extractor.xml";
                break;

            case AppFileType.ImportLog:
                path = @"{0}\{1}\import.log";
                break;

            case AppFileType.XsltAutoSave:
                path = @"{0}\{1}\autosave.xslt";
                break;

            default:
                throw new NotSupportedException("Unexpected app file path");
            }
            return(string.Format(path, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName));
        }
Exemplo n.º 14
0
        public static void OpenFile(AppFileType FileType, Action <string, AppFileType> opened)
        {
            using (var fbd = new CommonOpenFileDialog())
            {
                if (FileType == AppFileType.Video)
                {
                    fbd.Filters.Add(new CommonFileDialogFilter("视频文件", string.Join(";", VideoExts.ToList())));
                }
                else if (FileType == AppFileType.Sub)
                {
                    fbd.Filters.Add(new CommonFileDialogFilter("字幕文件", string.Join(";", SubExts.ToList())));
                }

                fbd.Filters.Add(new CommonFileDialogFilter("视频或字幕文件", string.Join(";", VideoExts.Concat(SubExts).ToList())));
                fbd.Filters.Add(new CommonFileDialogFilter("任何类型", "*.*"));
                var result = fbd.ShowDialog();

                if (result == CommonFileDialogResult.Ok && !string.IsNullOrWhiteSpace(fbd.FileName))
                {
                    var fileName = Path.GetFileName(fbd.FileName.Trim());
                    opened(fileName, FileType);
                }
            }
        }