/// <summary> /// 文件上传 /// </summary> private void Upload(HttpContext Context) { Hashtable FileTable = new Hashtable(); FileStream FileStream = default(FileStream); Stream Stream = default(Stream); byte[] ByteBuffer = {}; int ByteRead = 0; string Guid = ""; int Id = 0; int FolderId = 0; string FolderPath = ""; string CodeId = ""; string FilePath = ""; string FileName = ""; string FileExtension = ""; int FileSize = 0; string FileHash = ""; string FileType = ""; int FolderUserId = 0; string FolderUsername = ""; int FolderShare = 0; string TempStoragePath = ""; string TempFilePath = ""; string SaveStoragePath = ""; string SaveFilePath = ""; string Sql = ""; try { Guid = Context.Request.QueryString["Guid"].TypeString(); if (Base.Common.StringCheck(Guid, @"^[\w]{32}$") == false) { return; } if (Base.Common.IsNumeric(Context.Request.QueryString["FolderId"]) == true) { FolderId = Context.Request.QueryString["FolderId"].TypeInt(); } FilePath = Context.Request.QueryString["FilePath"].TypeString(); if (string.IsNullOrEmpty(FilePath) == true) { return; } FileName = Path.GetFileNameWithoutExtension(FilePath); if (Base.Common.StringCheck(FileName, @"^[^\\\/\:\*\?\""\<\>\|]{1,75}$") == false) { return; } FileExtension = Path.GetExtension(FilePath).ToString().ToLower(); if (Base.Common.IsNumeric(Context.Request.QueryString["FileSize"]) == true) { FileSize = Context.Request.QueryString["FileSize"].TypeInt(); } else { return; } FileHash = Context.Request.QueryString["FileHash"].TypeString(); if (Base.Common.StringCheck(FileHash, @"^[\w]{32}$") == false) { return; } if (FileSize > ConfigurationManager.AppSettings["UploadSize"].TypeInt() * 1024 * 1024) { return; } if (ExtensionCheck(FileExtension) == false) { return; } TempStoragePath = Context.Server.MapPath("/storage/file/temp/"); if (Directory.Exists(TempStoragePath) == false) { Directory.CreateDirectory(TempStoragePath); } TempFilePath = Base.Common.PathCombine(TempStoragePath, Guid); Stream = Context.Request.InputStream; FileStream = new FileStream(TempFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 4096, true); ByteBuffer = new byte[(int)Stream.Length]; ByteRead = Stream.Read(ByteBuffer, 0, (int)Stream.Length); FileStream.Write(ByteBuffer, 0, ByteRead); FileStream.Close(); FileStream.Dispose(); Stream.Close(); Stream.Dispose(); if (AppCommon.FileHash(TempFilePath) == FileHash) { Conn = Base.Data.DBConnection(ConfigurationManager.AppSettings["ConnectionString"].TypeString()); Conn.Open(); if (FolderId == 0) { FolderPath = "/0/"; FolderUserId = Context.Session["UserId"].TypeInt(); FolderUsername = Context.Session["Username"].TypeString(); } else { FolderPath = AppCommon.FolderIdPath(FolderId, ref Conn); if (AppCommon.PurviewCheck(FolderId, true, "uploader", ref Conn) == false) { return; } Base.Data.SqlDataToTable("Select DBS_Id, DBS_UserId, DBS_Username, DBS_Folder, DBS_Share, DBS_Lock, DBS_Recycle From DBS_File Where DBS_Folder = 1 And DBS_Lock = 0 And DBS_Recycle = 0 And DBS_Id = " + FolderId, ref Conn, ref FileTable); if (FileTable["Exist"].TypeBool() == false) { return; } else { FolderUserId = FileTable["DBS_UserId"].TypeInt(); FolderUsername = FileTable["DBS_Username"].TypeString(); FolderShare = FileTable["DBS_Share"].TypeInt(); } FileTable.Clear(); } CodeId = AppCommon.CodeId(); FileName = AppCommon.FileName(FolderId, FileName, FileExtension, ref Conn); FileType = AppCommon.FileType(FileExtension); SaveStoragePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1)); SaveFilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + FileExtension); if (Directory.Exists(SaveStoragePath) == false) { Directory.CreateDirectory(SaveStoragePath); } if (File.Exists(TempFilePath) == false) { return; } else { File.Move(TempFilePath, SaveFilePath); } Sql = "Insert Into DBS_File(DBS_UserId, DBS_Username, DBS_Version, DBS_VersionId, DBS_Folder, DBS_FolderId, DBS_FolderPath, DBS_CodeId, DBS_Hash, DBS_Name, DBS_Extension, DBS_Size, DBS_Type, DBS_Remark, DBS_Share, DBS_Lock, DBS_Sync, DBS_Recycle, DBS_CreateUsername, DBS_CreateTime, DBS_UpdateUsername, DBS_UpdateTime, DBS_RemoveUsername, DBS_RemoveTime) "; Sql += "Values(" + FolderUserId + ", '" + FolderUsername + "', 1, 0, 0, " + FolderId + ", '" + FolderPath + "', '" + CodeId + "', '" + FileHash + "', '" + FileName + "', '" + FileExtension + "', " + FileSize + ", '" + FileType + "', 'null', " + FolderShare + ", 0, 1, 0, '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', 'null', '1970/1/1 00:00:00')"; Id = Base.Data.SqlInsert(Sql, ref Conn); if (Id == 0) { return; } Base.Data.SqlQuery("Insert Into DBS_File_Process(DBS_FileId, DBS_Convert, DBS_Index) Values(" + Id + ", 1, 'add')", ref Conn); AppCommon.FileProcessTrigger(); AppCommon.Log(Id, "file-upload", ref Conn); Conn.Close(); Conn.Dispose(); } } catch (Exception ex) { File.Delete(TempFilePath); AppCommon.Error(ex); Context.Response.Write(ex.Message); } finally { if (Base.Common.IsNothing(FileStream) == false) { FileStream.Close(); FileStream.Dispose(); } if (Base.Common.IsNothing(Stream) == false) { Stream.Close(); Stream.Dispose(); } if (Base.Common.IsNothing(Conn) == false) { Conn.Close(); Conn.Dispose(); } GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// 文件版本上传 /// </summary> private void Upload(HttpContext Context) { Hashtable FileTable = new Hashtable(); HttpPostedFile UploadFile = default(HttpPostedFile); FileStream FileStream = default(FileStream); Stream Stream = default(Stream); byte[] ByteBuffer = {}; int ByteRead = 0; int Chunk = 0; int Chunks = 0; string Guid = ""; int FileId = 0; string Remark = ""; string FolderPath = ""; string Name = ""; string Extension = ""; string FileName = ""; string FileExtension = ""; int FileSize = 0; string TempStoragePath = ""; string TempFilePath = ""; string SaveStoragePath = ""; string SaveFilePath = ""; int NewId = 0; int NewVersion = 0; string NewCodeId = ""; string NewHash = ""; int VersionCount = 0; string Sql = ""; try { if (Base.Common.IsNumeric(Context.Request.Form["Chunk"]) == true) { Chunk = Context.Request.Form["Chunk"].TypeInt(); } else { return; } if (Base.Common.IsNumeric(Context.Request.Form["Chunks"]) == true) { Chunks = Context.Request.Form["Chunks"].TypeInt(); } else { return; } Guid = Context.Request.Form["Guid"].TypeString(); if (Base.Common.StringCheck(Guid, @"^[\d\.]+$") == false) { return; } if (Base.Common.IsNumeric(Context.Request.Form["FileId"]) == true) { FileId = Context.Request.Form["FileId"].TypeInt(); } else { return; } Remark = Base.Common.InputFilter(Context.Request.Form["Remark"].TypeString()); if (string.IsNullOrEmpty(Remark) == false) { if (Base.Common.StringCheck(Remark, @"^[\s\S]{1,100}$") == false) { return; } } UploadFile = Context.Request.Files[0]; if (Base.Common.IsNothing(UploadFile) == true || string.IsNullOrEmpty(UploadFile.FileName) == true || UploadFile.ContentLength == 0) { return; } FileName = Path.GetFileNameWithoutExtension(UploadFile.FileName); FileExtension = Path.GetExtension(UploadFile.FileName).ToString().ToLower(); if (Base.Common.IsNumeric(Context.Request.Form["Size"]) == true) { FileSize = Context.Request.Form["Size"].TypeInt(); } else { return; } if (FileSize > ConfigurationManager.AppSettings["UploadSize"].TypeInt() * 1024 * 1024) { return; } TempStoragePath = Context.Server.MapPath("/storage/file/temp/"); TempFilePath = Base.Common.PathCombine(TempStoragePath, Guid); Stream = UploadFile.InputStream; FileStream = new FileStream(TempFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 4096, true); ByteBuffer = new byte[(int)Stream.Length]; ByteRead = Stream.Read(ByteBuffer, 0, (int)Stream.Length); FileStream.Write(ByteBuffer, 0, ByteRead); FileStream.Close(); FileStream.Dispose(); Stream.Close(); Stream.Dispose(); if (Chunk == (Chunks == 0 ? 0 : Chunks - 1)) { if (AppCommon.PurviewCheck(FileId, false, "editor", ref Conn) == false) { return; } Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Lock, DBS_Recycle From DBS_File Where DBS_Folder = 0 And DBS_Lock = 0 And DBS_Recycle = 0 And DBS_Id = " + FileId, ref Conn, ref FileTable); if (FileTable["Exist"].TypeBool() == false) { return; } else { FolderPath = FileTable["DBS_FolderPath"].TypeString(); Name = FileTable["DBS_Name"].TypeString(); Extension = FileTable["DBS_Extension"].TypeString(); } FileTable.Clear(); if (Extension != FileExtension) { return; } NewVersion = AppCommon.FileVersionNumber(FileId, ref Conn); NewCodeId = AppCommon.CodeId(); SaveStoragePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1)); SaveFilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), NewCodeId + FileExtension); if (File.Exists(TempFilePath) == false) { return; } else { File.Move(TempFilePath, SaveFilePath); } VersionCount = Base.Data.SqlScalar("Select Count(*) From DBS_File Where DBS_VersionId = " + FileId, ref Conn); // 文件旧版本清理 if (VersionCount >= ConfigurationManager.AppSettings["VersionCount"].TypeInt()) { AppCommon.FileVersionCleanup(FileId, ref Conn); } NewHash = AppCommon.FileHash(SaveFilePath); Sql = "Insert Into DBS_File(DBS_UserId, DBS_Username, DBS_Version, DBS_VersionId, DBS_Folder, DBS_FolderId, DBS_FolderPath, DBS_CodeId, DBS_Hash, DBS_Name, DBS_Extension, DBS_Size, DBS_Type, DBS_Remark, DBS_Share, DBS_Lock, DBS_Sync, DBS_Recycle, DBS_CreateUsername, DBS_CreateTime, DBS_UpdateUsername, DBS_UpdateTime, DBS_RemoveUsername, DBS_RemoveTime) "; Sql += "Select DBS_UserId, DBS_Username, " + NewVersion + ", " + FileId + ", DBS_Folder, DBS_FolderId, DBS_FolderPath, '" + NewCodeId + "', '" + NewHash + "', '" + Name + "', DBS_Extension, " + FileSize + ", DBS_Type, '" + Remark + "', DBS_Share, DBS_Lock, DBS_Sync, DBS_Recycle, DBS_CreateUsername, DBS_CreateTime, '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', DBS_RemoveUsername, DBS_RemoveTime From DBS_File Where DBS_Id = " + FileId; NewId = Base.Data.SqlInsert(Sql, ref Conn); if (NewId == 0) { return; } Base.Data.SqlQuery("Insert Into DBS_File_Process(DBS_FileId, DBS_Convert, DBS_Index) Values(" + NewId + ", 1, 'null')", ref Conn); AppCommon.FileProcessTrigger(); AppCommon.Log(NewId, "file-upversion", ref Conn); } Context.Response.Write("success"); } catch (Exception ex) { File.Delete(TempFilePath); AppCommon.Error(ex); Context.Response.Write(ex.Message); } finally { if (Base.Common.IsNothing(FileStream) == false) { FileStream.Close(); FileStream.Dispose(); } if (Base.Common.IsNothing(Stream) == false) { Stream.Close(); Stream.Dispose(); } GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// 文件存储(入库) /// </summary> private void FileStorage(int FolderId, string FileName, string FileExtension, long FileSize, string FilePath, string StorageFolderPath, HttpContext Context) { Hashtable FileTable = new Hashtable(); int Id = 0; string FolderPath = ""; string CodeId = ""; string Hash = ""; string FileType = ""; string StorageFilePath = ""; string Sql = ""; Base.Data.SqlDataToTable("Select DBS_Id, DBS_UserId, DBS_Folder, DBS_FolderId, DBS_Name, DBS_Extension From DBS_File Where DBS_UserId = " + Context.Session["UserId"].TypeString() + " And DBS_Folder = 0 And DBS_FolderId = " + FolderId + " And DBS_Name = '" + FileName + "' And DBS_Extension = '" + FileExtension + "'", ref Conn, ref FileTable); if (FileTable["Exist"].TypeBool() == false) { Id = 0; } else { Id = FileTable["DBS_Id"].TypeInt(); } FileTable.Clear(); if (Id == 0) { if (FolderId == 0) { FolderPath = "/0/"; } else { FolderPath = AppCommon.FolderIdPath(FolderId, ref Conn); } CodeId = AppCommon.CodeId(); Hash = AppCommon.FileHash(FilePath); FileType = AppCommon.FileType(FileExtension); Sql = "Insert Into DBS_File(DBS_UserId, DBS_Username, DBS_Version, DBS_VersionId, DBS_Folder, DBS_FolderId, DBS_FolderPath, DBS_CodeId, DBS_Hash, DBS_Name, DBS_Extension, DBS_Size, DBS_Type, DBS_Remark, DBS_Share, DBS_Lock, DBS_Sync, DBS_Recycle, DBS_CreateUsername, DBS_CreateTime, DBS_UpdateUsername, DBS_UpdateTime, DBS_RemoveUsername, DBS_RemoveTime) "; Sql += "Values(" + Context.Session["UserId"].TypeString() + ", '" + Context.Session["Username"].TypeString() + "', 1, 0, 0, " + FolderId + ", '" + FolderPath + "', '" + CodeId + "', '" + Hash + "', '" + FileName + "', '" + FileExtension + "', " + FileSize + ", '" + FileType + "', 'null', 0, 0, 0, 0, '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', 'null', '1970/1/1 00:00:00')"; Id = Base.Data.SqlInsert(Sql, ref Conn); if (Id == 0) { return; } StorageFilePath = Base.Common.PathCombine(StorageFolderPath, CodeId + FileExtension); File.Move(FilePath, StorageFilePath); Base.Data.SqlQuery("Insert Into DBS_File_Process(DBS_FileId, DBS_Convert, DBS_Index) Values(" + Id + ", 1, 'add')", ref Conn); AppCommon.FileProcessTrigger(); AppCommon.Log(Id, "file-add", ref Conn); } }
/// <summary> /// 文件复制 /// </summary> private void Copy(HttpContext Context) { Hashtable FileTable = new Hashtable(); byte[] Bytes = {}; int Id = 0; string FolderPath = ""; string CodeId = ""; string Name = ""; string Extension = ""; int FolderId = 0; string FolderIdPath = ""; int FolderUserId = 0; string FolderUsername = ""; int FolderShare = 0; int FolderSync = 0; int NewId = 0; string NewCodeId = ""; string NewName = ""; string SourceFilePath = ""; string TargetFilePath = ""; string Sql = ""; if (Base.Common.IsNumeric(Context.Request.Form["Id"]) == true) { Id = Context.Request.Form["Id"].TypeInt(); } else { return; } if (Base.Common.IsNumeric(Context.Request.Form["FolderId"]) == true) { FolderId = Context.Request.Form["FolderId"].TypeInt(); } else { return; } if (AppCommon.PurviewCheck(Id, false, "editor", ref Conn) == false) { Context.Response.Write("no-permission"); return; } Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderId, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Lock From DBS_File Where DBS_Folder = 0 And DBS_Lock = 0 And DBS_Id = " + Id, ref Conn, ref FileTable); if (FileTable["Exist"].TypeBool() == false) { return; } else { FolderPath = FileTable["DBS_FolderPath"].TypeString(); CodeId = FileTable["DBS_CodeId"].TypeString(); Name = FileTable["DBS_Name"].TypeString(); Extension = FileTable["DBS_Extension"].TypeString(); } FileTable.Clear(); if (FolderId == 0) { FolderIdPath = "/0/"; FolderUserId = Context.Session["UserId"].TypeInt(); FolderUsername = Context.Session["Username"].TypeString(); } else { FolderIdPath = AppCommon.FolderIdPath(FolderId, ref Conn); if (AppCommon.PurviewCheck(FolderId, true, "uploader", ref Conn) == false) { Context.Response.Write("no-permission"); return; } Base.Data.SqlDataToTable("Select DBS_Id, DBS_UserId, DBS_Username, DBS_Folder, DBS_Share, DBS_Lock, DBS_Sync From DBS_File Where DBS_Folder = 1 And DBS_Lock = 0 And DBS_Id = " + FolderId, ref Conn, ref FileTable); if (FileTable["Exist"].TypeBool() == false) { return; } else { FolderUserId = FileTable["DBS_UserId"].TypeInt(); FolderUsername = FileTable["DBS_Username"].TypeString(); FolderShare = FileTable["DBS_Share"].TypeInt(); FolderSync = FileTable["DBS_Sync"].TypeInt(); } FileTable.Clear(); } NewCodeId = AppCommon.CodeId(); NewName = AppCommon.FileName(FolderId, Name, Extension, ref Conn); SourceFilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension); TargetFilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderIdPath.Substring(1), NewCodeId + Extension); if (File.Exists(SourceFilePath) == false) { return; } else { Bytes = Base.Crypto.FileDecrypt(SourceFilePath, CodeId, true, false, true); if (Base.Common.IsNothing(Bytes) == true) { return; } else { File.WriteAllBytes(TargetFilePath, Bytes); } } Sql = "Insert Into DBS_File(DBS_UserId, DBS_Username, DBS_Version, DBS_VersionId, DBS_Folder, DBS_FolderId, DBS_FolderPath, DBS_CodeId, DBS_Hash, DBS_Name, DBS_Extension, DBS_Size, DBS_Type, DBS_Remark, DBS_Share, DBS_Lock, DBS_Sync, DBS_Recycle, DBS_CreateUsername, DBS_CreateTime, DBS_UpdateUsername, DBS_UpdateTime, DBS_RemoveUsername, DBS_RemoveTime) "; Sql += "Select DBS_UserId, DBS_Username, 1, 0, 0, " + FolderId + ", '" + FolderIdPath + "', '" + NewCodeId + "', DBS_Hash, '" + NewName + "', DBS_Extension, DBS_Size, DBS_Type, 'null', " + FolderShare + ", 0, " + FolderSync + ", 0, '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', '" + Context.Session["Username"].TypeString() + "', '" + DateTime.Now.ToString() + "', 'null', '1970/1/1 00:00:00' From DBS_File Where DBS_Id = " + Id; NewId = Base.Data.SqlInsert(Sql, ref Conn); if (NewId == 0) { return; } Base.Data.SqlQuery("Insert Into DBS_File_Process(DBS_FileId, DBS_Convert, DBS_Index) Values(" + NewId + ", 1, 'add')", ref Conn); AppCommon.FileProcessTrigger(); AppCommon.Log(NewId, "file-copy", ref Conn); Context.Response.Write("complete"); }