Example #1
0
 /// <summary>
 ///下载WebService所在服务器上的文件的方法 (生成临时文件)
 /// </summary>
 /// <param name="FileId">文件ID</param>
 /// <returns></returns>
 public string DownFileByScratch(string FileId)
 {
     using (FileCenterDbContext dbContext = new FileCenterDbContext())
     {
         int Id = 0;
         int.TryParse(FileId, out Id);
         var systemFiles = dbContext.SystemFiles.FirstOrDefault(s => s.Id == Id);
         if (systemFiles != null)
         {
             if (systemFiles.FilePath.Length == 0 && systemFiles.FileData.Length > 0)
             {
                 var    FolderName = ConfigurationManager.AppSettings["ScratchFile"];
                 string filePath   = Server.MapPath(FolderName);
                 if (Directory.Exists(filePath) == false)//如果不存在就创建file文件夹
                 {
                     Directory.CreateDirectory(filePath);
                 }
                 string     SaveFileName = Guid.NewGuid().ToString() + systemFiles.FileExtension; // 保存文件名称
                 FileStream fs           = File.Create(filePath + "/" + SaveFileName);
                 byte[]     Data         = systemFiles.FileData;
                 fs.Write(Data, 0, Data.Length);
                 fs.Close();
                 systemFiles.FilePath = FolderName + "/" + SaveFileName;
             }
             return(Newtonsoft.Json.JsonConvert.SerializeObject(systemFiles));
         }
         else
         {
             return("");
         }
     }
 }
Example #2
0
        private List <Operate> GetOperateList()
        {
            // 预先将当前计划中的月份新增到数据库
            var dayOperationList = new List <Operate>();

            var startDate = GetCompressStartDate().Date;

            var endDate = startDate.AddDays(1).Date;

            using (var context = new FileCenterDbContext())
            {
                context.Operates.Add(new Operate
                {
                    CreationTime  = DateTime.Now,
                    OperateResult = EnumOperateResult.Failure,
                    OperateType   = EnumOperateType.ImageCompress,
                    StartDate     = startDate,
                    EndDate       = endDate
                });

                context.SaveChanges();
            }

            using (FileCenterDbContext fileCenterDbContext = new FileCenterDbContext())
            {
                dayOperationList = fileCenterDbContext.Operates.AsNoTracking().Where(s => s.OperateType == EnumOperateType.ImageCompress && s.OperateResult == EnumOperateResult.Failure).ToList();
            }

            return(dayOperationList);
        }
Example #3
0
        public FileResult DownloadSingleFileById(string FileId, bool IsConvertStream = false)
        {
            FileResult Result = new FileResult();

            try
            {
                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    SystemFiles systemFiles = null;
                    int         Id          = 0;
                    int.TryParse(FileId, out Id);
                    systemFiles = dbContext.SystemFiles.FirstOrDefault(s => s.Id == Id);

                    var filePath = this.GetFilePath(systemFiles);

                    if (IsConvertStream)
                    {
                        systemFiles.FileData = File.ReadAllBytes(filePath);
                    }

                    Result.Status  = "1";
                    Result.Message = "SUCCESS";
                    Result.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(systemFiles);
                    return(Result);
                }
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                Result.Result  = null;
                return(Result);
            }
        }
Example #4
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="FileId"></param>
        /// <returns></returns>
        public FileResult DeleteFile(string FileId)
        {
            FileResult Result = new FileResult();

            try
            {
                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    SystemFiles systemFiles = null;
                    int         Id          = 0;
                    int.TryParse(FileId, out Id);
                    systemFiles = dbContext.SystemFiles.Find(Id);

                    if (systemFiles != null)
                    {
                        systemFiles.IsDelete = 1;
                    }

                    Result.Status  = "1";
                    Result.Message = "SUCCESS";
                    Result.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(systemFiles);
                    return(Result);
                }
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                Result.Result  = null;
                return(Result);
            }
        }
Example #5
0
        public FileResult DeleteMoreFile(List <string> ids)
        {
            FileResult Result = new FileResult();

            try
            {
                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    var deleteFileList = (from file in dbContext.SystemFiles
                                          where file.IsDelete == null && ids.Any(s => s == file.FileId.ToString())
                                          select new SystemFilesView
                    {
                        Id = file.Id,
                        FileName = file.FileName,
                        FileLongName = file.FileLongName,
                        FileExtension = file.FileExtension,
                        FileSize = file.FileSize,
                        FileId = file.FileId,
                        FilePath = file.FilePath,
                        ProjectName = file.ProjectName
                    }).ToList();

                    if (deleteFileList.Any())
                    {
                        foreach (var item in deleteFileList)
                        {
                            item.IsDelete = 1;

                            var filepath = Server.MapPath(".") + "/" + item.FilePath;

                            if (File.Exists(filepath))
                            {
                                try
                                {
                                    File.Delete(filepath);
                                }
                                catch
                                {
                                }
                            }
                            dbContext.SaveChanges();
                        }
                    }

                    Result.Status  = "1";
                    Result.Message = "SUCCESS";
                    return(Result);
                }
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                Result.Result  = null;
                return(Result);
            }
        }
Example #6
0
        public FileResult GetFileTokenList(string jsonParameter)
        {
            LogHelper.Info($"GetFileTokenList Start At:{DateTime.Now}");
            var Result        = new FileResult();
            var fileTokenList = new List <FileToken>();
            var systemFiles   = new SystemFiles();

            try
            {
                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    var passedParamList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <FileToken> >(jsonParameter);

                    foreach (var item in passedParamList)
                    {
                        // 解密获取真正的FileId
                        item.FileId = rsaManager.RSADecrypt(File.ReadAllText(AppConfig.PrivateKeyFilePath), item.FileId);
                        item.Token  = this.GenerateToken(item);
                        item.Url    = $"{AppConfig.FileCenterDomain}/file/GetFile?token={HttpUtility.UrlEncode(item.Token)}";

                        int Id = 0;
                        if (int.TryParse(item.FileId, out Id))
                        {
                            // 传入的id是int类型,说明是Id字段
                            systemFiles = dbContext.SystemFiles.FirstOrDefault(s => s.Id == Id);
                        }
                        else
                        {
                            // 传入的id不是int类型,说明是FileId字段
                            systemFiles = dbContext.SystemFiles.FirstOrDefault(s => s.FileId == new Guid(item.FileId));
                        }

                        item.FileName = systemFiles.FileName;

                        cacheManager.Set(item.Token, item.FileId);
                    }

                    Result.Status  = "1";
                    Result.Message = "SUCCESS";
                    Result.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(passedParamList);
                    return(Result);
                }
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                return(Result);
            }
            finally
            {
                LogHelper.Info($"GetFileTokenList End At:{DateTime.Now}");
            }
        }
Example #7
0
        public FileResult UploadFileSwitch(byte[] FileData, string FileName, string FileExtension, string ProjectNo, string SaveType, int IsPdf)
        {
            if (!string.IsNullOrEmpty(FileExtension))
            {
                FileExtension = Path.GetExtension(FileName);
            }

            var Result = UpLoadFile(FileData, FileName, FileExtension, ProjectNo, SaveType);

            if (IsPdf == 0) // 图片不需要再转成pdf文件
            {
                return(Result);
            }
            else
            {
                #region 转成PDF文件
                var    file     = Newtonsoft.Json.JsonConvert.DeserializeObject <SystemFiles>(Result.Result);
                string savePath = file.FilePath;
                string fileName = file.FileName;
                var    pdfPath  = AttachmentFile2PDF(ref savePath, ref fileName, Path.Combine(AppConfig.FileUploadRootDirectory, savePath));

                var systemFiles = new SystemFiles()
                {
                    FileId        = Guid.NewGuid(),
                    FileData      = null,
                    FilePath      = pdfPath,
                    FileExtension = ".pdf",
                    FileName      = FileName + ".pdf",
                    FileLongName  = pdfPath,
                    FileSize      = FileData.Length,
                    ProjectName   = ProjectNo,
                    CreateDate    = DateTime.Now
                };

                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    dbContext.SystemFiles.Add(systemFiles);
                    dbContext.SaveChanges();
                }

                file.PdfFileId = systemFiles.FileId;

                var result1 = new FileResult();
                result1.Status  = "1";
                result1.Message = "SUCCESS";
                result1.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(file);
                return(result1);

                #endregion
            }
        }
Example #8
0
        /// <summary>
        /// 记录完成的月份,存在的更新操作成功,新增的记录操作成功
        /// </summary>
        /// <param name="monthOperationList"></param>
        private void RecordOperatedDayToSuccess(Operate operate)
        {
            using (var context = new FileCenterDbContext())
            {
                var entity = context.Operates.FirstOrDefault(s => s.Id == operate.Id);

                if (entity != null)
                {
                    entity.OperateResult = EnumOperateResult.Success;
                }

                context.SaveChanges();
            }
        }
Example #9
0
        public FileResult DownloadMultiFileById(List <int> ids)
        {
            FileResult             Result = new FileResult();
            List <SystemFilesView> list;

            try
            {
                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    list = (from file in dbContext.SystemFiles
                            where file.IsDelete == null && ids.Any(s => s == file.Id)
                            select new SystemFilesView
                    {
                        Id = file.Id,
                        FileName = file.FileName,
                        FileLongName = file.FileLongName,
                        FileExtension = file.FileExtension,
                        FileSize = file.FileSize,
                        FileId = file.FileId,
                        FilePath = file.FilePath,
                        ProjectName = file.ProjectName
                    }).ToList();

                    foreach (var item in list)
                    {
                        var filePath = this.GetFilePath(item);

                        byte[] fileData = File.ReadAllBytes(filePath);

                        item.FileData = fileData;
                    }

                    Result.Status  = "1";
                    Result.Message = "SUCCESS";
                    Result.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                    return(Result);
                }
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                Result.Result  = null;
                return(Result);
            }
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="projectNo"></param>
        /// <param name="localImagePath"></param>
        /// <param name="thumbnailImagePath"></param>
        /// <returns></returns>
        private SystemFiles GetThumbnail(string projectNo, string folder, string localImagePath, string thumbnailImagePath)
        {
            try
            {
                var systemFiles = new SystemFiles();
                var OldPath     = this.GetFilePath(new SystemFiles()
                {
                    FilePath = folder + "/" + localImagePath
                });                                                                                           //Server.MapPath(folder) + "/" + localImagePath;
                var covertPath = Path.Combine(AppConfig.FileUploadRootDirectory, folder, thumbnailImagePath); // Server.MapPath(folder) + "/" + thumbnailImagePath;
                var success    = GetThumbnail(OldPath, covertPath, 180, 140);

                if (success)
                {
                    var path = folder + "/" + thumbnailImagePath;

                    systemFiles = new SystemFiles()
                    {
                        FileId        = Guid.NewGuid(),
                        FileData      = null,
                        FilePath      = path,
                        FileExtension = Path.GetExtension(path),
                        FileName      = Path.GetFileName(path),
                        FileLongName  = Path.GetFileName(path),
                        FileSize      = 0,
                        ProjectName   = projectNo,
                        CreateDate    = DateTime.Now
                    };

                    using (FileCenterDbContext dbContext = new FileCenterDbContext())
                    {
                        dbContext.SystemFiles.Add(systemFiles);
                        dbContext.SaveChanges();
                    }
                }

                return(systemFiles);
            }
            catch (Exception ex)
            {
                return(new SystemFiles());
            }
        }
Example #11
0
        /// <summary>
        /// 解压文件(By Id)
        /// </summary>
        private void DepressFile(List <int> ids)
        {
            using (var _dbContext = new FileCenterDbContext())
            {
                var list = _dbContext.SystemFiles.Where(s => ids.Contains(s.Id)).ToList();

                foreach (var item in list)
                {
                    var fullPath = $"{AppConfig.FileUploadRootDirectory.Replace("\\", "/")}/{item.FilePath}";

                    if (!File.Exists(fullPath))
                    {
                        ZipFileManager.DepressFile(Path.Combine(AppConfig.FileArchiveRootDirectory, item.ArchiveFileName), item.FileLongName, fullPath);

                        item.FileCompressStatus = EnumFileCompressStatus.Uncompressed;
                        item.ArchiveFileName    = string.Empty;
                        item.DecompressDateTime = DateTime.Now;
                    }
                }

                _dbContext.SaveChanges();
            }
        }
Example #12
0
        public FileResult UploadFile(byte[] FileData, string FileName, string FileExtension, string ProjectNo, string SaveType)
        {
            var Result = new FileResult();

            try
            {
                SystemFiles systemFiles = new SystemFiles();

                var FolderName = ConfigurationManager.AppSettings[ProjectNo];

                if (FolderName == null)
                {
                    FolderName = ProjectNo;
                }

                string day      = DateTime.Now.ToString("yyyyMMdd");
                string folder   = FolderName + "/" + day;
                string filePath = Server.MapPath(folder);

                if (Directory.Exists(filePath) == false)// 如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(filePath);
                }
                string longName     = Guid.NewGuid().ToString();
                string SaveFileName = longName + FileExtension; // 保存文件名称

                using (FileStream fs = File.Create(filePath + "/" + SaveFileName))
                {
                    fs.Write(FileData, 0, FileData.Length);
                }

                string smallPicPath = longName + "_100.100" + FileExtension;

                systemFiles = new SystemFiles()
                {
                    FileId             = Guid.NewGuid(),
                    FileData           = null,
                    FilePath           = folder + "/" + SaveFileName,
                    FileExtension      = FileExtension,
                    FileName           = FileName,
                    FileLongName       = SaveFileName,
                    FileSize           = FileData.Length,
                    ProjectName        = ProjectNo,
                    CreateDate         = DateTime.Now,
                    FileCompressStatus = EnumFileCompressStatus.Uncompressed
                };

                if (AppConfig.ImageExtension.Contains(FileExtension.ToLower())) // 如果是图片,自动增加缩略图转换
                {
                    var coverName = Guid.NewGuid().ToString() + FileExtension;  // 保存文件名称;
                    var cover     = GetThumbnail(ProjectNo, folder, SaveFileName, coverName);
                    if (cover.FileId.HasValue)
                    {
                        systemFiles.CoverSavePath = cover.FilePath;
                        systemFiles.CoverFileId   = cover.FileId;
                    }
                }

                using (FileCenterDbContext dbContext = new FileCenterDbContext())
                {
                    dbContext.SystemFiles.Add(systemFiles);
                    dbContext.SaveChanges();
                }

                //systemFiles.FileData = FileData;// 将二进制流返回

                Result.Status  = "1";
                Result.Message = "SUCCESS";
                Result.Result  = Newtonsoft.Json.JsonConvert.SerializeObject(systemFiles);

                return(Result);
            }
            catch (Exception er)
            {
                Result.Status  = "0";
                Result.Message = er.Message;
                return(Result);
            }
        }
Example #13
0
        private void DownloadFile(HttpContext context)
        {
            var fileId = context.Request.QueryString["fileId"];

            if (string.IsNullOrWhiteSpace(fileId))
            {
                context.Response.StatusCode = 404;
                return;
            }
            var projectName = context.Request.QueryString["projectName"];

            if (string.IsNullOrWhiteSpace(projectName))
            {
                projectName = context.Request.QueryString["project"];
            }
            if (string.IsNullOrWhiteSpace(projectName))
            {
                context.Response.StatusCode = 404;
                return;
            }
            string key = GetKey(projectName);

            if (string.IsNullOrWhiteSpace(key))
            {
                context.Response.StatusCode = 404;
                return;
            }
            try
            {
                fileId = AESHelper.AESDecrypt(fileId, key);
            }
            catch
            {
                context.Response.StatusCode = 404;
                return;
            }
            int id = 0;

            if (!int.TryParse(fileId, out id))
            {
                context.Response.StatusCode = 404;
                return;
            }

            //通过文件ID获取出文件
            using (var _dbContext = new FileCenterDbContext())
            {
                var file = _dbContext.SystemFiles.FirstOrDefault(m => m.Id == id && m.ProjectName == projectName);

                this.DepressFile(new List <int>(file.Id));

                var fileName = file.FileName;

                if (!string.IsNullOrWhiteSpace(context.Request.QueryString["rename"]))
                {
                    fileName = Guid.NewGuid().ToString("N");
                }

                if (string.IsNullOrWhiteSpace(file.FilePath))
                {
                    ResponseFile(context, fileName, file.FileData);
                }
                else
                {
                    ResponseFile(context, fileName, file.FilePath);
                }
            }
        }