Ejemplo n.º 1
0
        /// <summary>
        /// 关联文件到对应的数据,并移动到指定的路径,并保存数据到数据库返回File对象
        /// </summary>
        /// <param name="interimPath"></param>
        /// <param name="categoryId"></param>
        /// <param name="categoryType"></param>
        /// <param name="currentUserId"></param>
        /// <param name="isReplace">是否替换掉之前所有相同类型(categoryType)的文件</param>
        /// <param name="customSaveDirPath"></param>
        /// <returns></returns>
        public Files BindUploadFiles(string interimPath, string categoryId, FileTypesEnum categoryType, long currentUserId, bool isReplace = true, string customSaveDirPath = "")
        {
            string interimFullPath = WebHelper.GetMapPath(interimPath);

            System.IO.FileInfo fi = new System.IO.FileInfo(interimFullPath);
            if (fi.Exists)
            {
                var fileHandler = DealWithUploadFileHelper.GetInstance();
                try
                {
                    string fileSourceName = string.Empty; //文件名
                    string fileName       = string.Empty; //文件路径
                    //当包含]时为新传文件
                    //var f = fi.Name.IndexOf("]") > -1;
                    if (fi.Name.IndexOf(']') < 1)
                    {
                        return(null);
                    }
                    int index = fi.Name.LastIndexOf(']');
                    fileName       = fi.Name.Substring(index + 1);
                    fileSourceName = fi.Name.Substring(0, index).TrimStart('[');
                    string oldPath  = string.Empty;
                    bool   isCreate = false;
                    Files  model    = null;
                    if (isReplace && categoryId.IsNotNullAndNotEmpty())
                    {
                        model = _fileAppService.GetFilesByCategoryIdAndType(categoryId, categoryType.ToString());
                        if (model != null)
                        {
                            if (model.Url == interimPath)
                            {
                                return(null);
                            }
                            model.SortNo = 0;
                            oldPath      = model.OldUrl;
                            model.OldUrl = model.Url;
                        }
                    }
                    if (model == null)
                    {
                        isCreate = true;
                        model    = new Files();
                        //model.Id = IdentityCreator.NewGuid;
                        model.CreationTime  = DateTime.Now;
                        model.RelateId      = categoryId;
                        model.CategoryType  = categoryType.ToString();
                        model.OldUrl        = "";
                        model.SortNo        = 0;
                        model.CreatorUserId = currentUserId;
                        model.TenantId      = 1;
                    }
                    model.FileName = fileSourceName.Substring(0, fileSourceName.Length > 100 ? 100 : fileSourceName.Length);
                    model.FileSize = (int)fi.Length;

                    string cPath = string.Empty;
                    if (customSaveDirPath.IsNullOrEmpty())
                    {
                        string baseDirPath = DealWithUploadFileHelper.GetInstance().GetFilesSaveDirPath(categoryType.ToString());//保存文件夹路径,路径是从根目录开始
                        cPath = baseDirPath;
                    }
                    else
                    {
                        cPath = customSaveDirPath;
                    }
                    model.Url = System.IO.Path.Combine(cPath, fileName);
                    cPath     = WebHelper.GetMapPath(cPath);//保存文件夹完整路径
                    if (!System.IO.Directory.Exists(cPath))
                    {
                        System.IO.Directory.CreateDirectory(cPath);
                    }
                    if (isCreate)
                    {
                        model = _fileAppService.Insert(model);
                    }
                    else
                    {
                        model = _fileAppService.Update(model);
                    }
                    cPath = WebHelper.GetMapPath(model.Url);
                    if (!System.IO.File.Exists(cPath))
                    {
                        fi.MoveTo(cPath);
                    }
                    #region 视频的封面处理

                    string videoCoverImg = string.Empty;
                    if (System.IO.Path.GetExtension(fi.FullName).ToUpper().Contains("MP4"))
                    {
                        string dir                = System.IO.Path.GetDirectoryName(interimFullPath);
                        string videoFileName      = System.IO.Path.GetFileNameWithoutExtension(cPath) + ".jpg";
                        string coverSourceImgPath = string.Concat(dir, "\\", videoFileName);
                        if (System.IO.File.Exists(coverSourceImgPath))
                        {
                            string saveCoverPath = string.Concat(System.IO.Path.GetDirectoryName(cPath), "\\", videoFileName);
                            new System.IO.FileInfo(coverSourceImgPath).MoveTo(saveCoverPath);
                            MakeThumbnail(saveCoverPath, categoryType);//封面裁切成指定尺寸
                        }
                    }

                    #endregion
                    #region 处理上传图片,需要缩略图等多种尺寸

                    #region 生成缩略图

                    MakeThumbnail(cPath, categoryType);

                    #endregion
                    #endregion
                    //更新删除旧文件
                    if (oldPath.IsNotNullAndNotEmpty())
                    {
                        MoveToRecycleAsync(oldPath);
                    }
                    return(model);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                }
            }
            return(null);
        }