Exemplo n.º 1
0
        /// <summary>
        /// 打包文件流输出
        /// </summary>
        private void OutputZip(string PackageFilePath, HttpContext Context)
        {
            string FileName = "";

            try
            {
                FileName = "" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip";

                Context.Response.Clear();
                Context.Response.Buffer       = false;
                Context.Response.BufferOutput = false;
                Context.Response.ContentType  = "application/octet-stream";
                Context.Response.AddHeader("Content-Transfer-Encoding", "binary");
                Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + "");
                Context.Response.TransmitFile(PackageFilePath);
                Context.Response.Flush();
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                File.Delete(PackageFilePath);

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Exemplo n.º 2
0
        /// <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();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 文件下载
        /// </summary>
        private void Download(HttpContext Context)
        {
            Hashtable FileTable  = new Hashtable();
            int       Id         = 0;
            string    FolderPath = "";
            string    CodeId     = "";
            string    Name       = "";
            string    Extension  = "";
            string    FilePath   = "";

            try
            {
                if (Base.Common.IsNumeric(Context.Request.QueryString["Id"]) == true)
                {
                    Id = Context.Request.QueryString["Id"].TypeInt();
                }
                else
                {
                    Context.Response.StatusCode = 500;
                    return;
                }

                CodeId = Context.Request.QueryString["CodeId"].TypeString();

                if (Base.Common.StringCheck(CodeId, @"^[\d]{8}-[\d]{6}-[\d]{7}-[\d]{3}$") == false)
                {
                    Context.Response.StatusCode = 500;
                    return;
                }

                if (AppCommon.PurviewCheck(Id, false, "downloader", ref Conn) == false)
                {
                    Context.Response.StatusCode = 500;
                    return;
                }

                Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Recycle From DBS_File Where DBS_Folder = 0 And DBS_Id = " + Id + " And DBS_CodeId = '" + CodeId + "' And DBS_Recycle = 0", ref Conn, ref FileTable);

                if (FileTable["Exist"].TypeBool() == false)
                {
                    Context.Response.StatusCode = 404;
                    return;
                }
                else
                {
                    FolderPath = FileTable["DBS_FolderPath"].TypeString();
                    Name       = FileTable["DBS_Name"].TypeString();
                    Extension  = FileTable["DBS_Extension"].TypeString();
                }

                FileTable.Clear();

                FilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension);

                if (File.Exists(FilePath) == false)
                {
                    Context.Response.StatusCode = 404;
                    return;
                }

                Output(CodeId, Name, Extension, FilePath, Context);

                AppCommon.Log(Id, "file-download", ref Conn);
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 文件流输出
        /// </summary>
        private void Output(string CodeId, string Name, string Extension, string FilePath, HttpContext Context)
        {
            MemoryStream MS = default(MemoryStream);

            byte[] Bytes  = {};
            byte[] Buffer = {};
            int    Count  = 0;

            try
            {
                Bytes = Base.Crypto.FileDecrypt(FilePath, CodeId, true, false, true);

                if (Base.Common.IsNothing(Bytes) == true)
                {
                    Bytes = File.ReadAllBytes(FilePath);
                }

                MS = new MemoryStream(Bytes);

                Buffer = new byte[1024];

                Count = MS.Read(Buffer, 0, 1024);

                Context.Response.Clear();
                Context.Response.Buffer       = false;
                Context.Response.BufferOutput = false;
                Context.Response.ContentType  = "application/octet-stream";
                Context.Response.AddHeader("Content-Transfer-Encoding", "binary");

                while (Count > 0)
                {
                    Context.Response.OutputStream.Write(Buffer, 0, Count);

                    if (Context.Response.IsClientConnected == true)
                    {
                        Context.Response.Flush();
                    }
                    else
                    {
                        Context.Response.End();
                    }

                    Count = MS.Read(Buffer, 0, 1024);
                }

                Bytes = null;

                MS.Dispose();
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                if (Base.Common.IsNothing(MS) == false)
                {
                    MS.Dispose();
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 压缩文件解包
        /// </summary>
        private void ZipUnpack(HttpContext Context)
        {
            MemoryStream      MS                = default(MemoryStream);
            SevenZipExtractor Extractor         = default(SevenZipExtractor);
            Hashtable         ExportTable       = new Hashtable();
            Hashtable         FileTable         = new Hashtable();
            string            PackageId         = "";
            string            PackageFolderPath = "";
            string            UnpackFolderPath  = "";
            string            StorageFolderPath = "";
            int    Id         = 0;
            string FolderPath = "";
            string CodeId     = "";
            string Name       = "";
            string Extension  = "";
            string FilePath   = "";
            string Password   = "";

            byte[] Bytes = {};
            int    Item  = 0;
            int    Index = 0;

            PackageId = Guid.NewGuid().ToString();

            PackageFolderPath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/temp/"), PackageId);

            if (Directory.Exists(PackageFolderPath) == false)
            {
                Directory.CreateDirectory(PackageFolderPath);
            }

            StorageFolderPath = Context.Server.MapPath("/storage/file/");

            try
            {
                if (Base.Common.IsNumeric(Context.Request.QueryString["Id"]) == true)
                {
                    Id = Context.Request.QueryString["Id"].TypeInt();
                }
                else
                {
                    return;
                }

                Password = Context.Request.QueryString["Password"].TypeString();

                if (string.IsNullOrEmpty(Password) == false)
                {
                    if (Base.Common.StringCheck(Password, @"^[\S]{1,32}$") == false)
                    {
                        return;
                    }
                }

                if (AppCommon.PurviewCheck(Id, false, "downloader", ref Conn) == false)
                {
                    Context.Response.Write("no-permission");
                    return;
                }

                for (Index = 0; Index < Context.Request.QueryString.GetValues("Item").Length; Index++)
                {
                    if (Base.Common.IsNumeric(Context.Request.QueryString.GetValues("Item")[Index]) == true)
                    {
                        Item = Context.Request.QueryString.GetValues("Item")[Index].TypeInt();
                    }
                    else
                    {
                        continue;
                    }

                    ExportTable.Add(Item, true);
                }

                Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Recycle From DBS_File Where DBS_Folder = 0 And DBS_Recycle = 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 (Extension != ".7z" && Extension != ".rar" && Extension != ".zip")
                {
                    return;
                }

                FilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension);

                if (File.Exists(FilePath) == false)
                {
                    return;
                }

                UnpackFolderPath = Base.Common.PathCombine(PackageFolderPath, Name);

                if (Directory.Exists(UnpackFolderPath) == false)
                {
                    Directory.CreateDirectory(UnpackFolderPath);
                }

                Bytes = ReadFileBytes(FilePath, CodeId);

                if (Bytes.Length == 0)
                {
                    return;
                }

                MS = new MemoryStream(Bytes);

                SevenZipCompressor.SetLibraryPath(Context.Server.MapPath("/bin/7z64.dll"));

                if (string.IsNullOrEmpty(Password) == true)
                {
                    Extractor = new SevenZipExtractor(MS);
                }
                else
                {
                    Extractor = new SevenZipExtractor(MS, Password);
                }

                for (Index = 0; Index < Extractor.ArchiveFileData.Count; Index++)
                {
                    if (Context.Response.IsClientConnected == false)
                    {
                        return;
                    }

                    if (ExportTable[Index].TypeBool() == true)
                    {
                        try
                        {
                            Extractor.ExtractFiles(UnpackFolderPath, Extractor.ArchiveFileData[Index].Index);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

                if (Extractor.Check() == false)
                {
                    Context.Response.Write("wrong-password");
                    return;
                }

                Extractor = null;

                MS.Dispose();

                ZipScan(0, PackageFolderPath, StorageFolderPath, Context);
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                if (Base.Common.IsNothing(MS) == false)
                {
                    MS.Dispose();
                }

                if (Base.Common.IsNothing(Extractor) == false)
                {
                    Extractor = null;
                }

                Directory.Delete(PackageFolderPath, true);

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
        /// <summary>
        /// 文件打包下载
        /// </summary>
        private void DownloadPackage(HttpContext Context)
        {
            SevenZipCompressor Compressor        = default(SevenZipCompressor);
            Hashtable          FileTable         = new Hashtable();
            string             PackageId         = "";
            string             PackageFolderPath = "";
            string             PackageFilePath   = "";
            int    Id          = 0;
            int    Folder      = 0;
            string FolderPath  = "";
            string CodeId      = "";
            string Name        = "";
            string Extension   = "";
            string StoragePath = "";
            string FilePath    = "";
            int    Index       = 0;

            PackageId = Guid.NewGuid().ToString();

            PackageFolderPath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/temp/"), PackageId);

            if (Directory.Exists(PackageFolderPath) == false)
            {
                Directory.CreateDirectory(PackageFolderPath);
            }

            PackageFilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/temp/"), "" + PackageId + ".zip");

            try
            {
                if (Context.Request.QueryString.GetValues("Id").Length == 0)
                {
                    return;
                }

                for (Index = 0; Index < Context.Request.QueryString.GetValues("Id").Length; Index++)
                {
                    if (Context.Response.IsClientConnected == false)
                    {
                        return;
                    }

                    if (Base.Common.IsNumeric(Context.Request.QueryString.GetValues("Id")[Index]) == true)
                    {
                        Id = Context.Request.QueryString.GetValues("Id")[Index].TypeInt();
                    }
                    else
                    {
                        continue;
                    }

                    Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Recycle From DBS_File Where DBS_Recycle = 0 And DBS_Id = " + Id, ref Conn, ref FileTable);

                    if (FileTable["Exist"].TypeBool() == false)
                    {
                        continue;
                    }
                    else
                    {
                        Folder     = FileTable["DBS_Folder"].TypeInt();
                        FolderPath = FileTable["DBS_FolderPath"].TypeString();
                        CodeId     = FileTable["DBS_CodeId"].TypeString();
                        Name       = FileTable["DBS_Name"].TypeString();
                        Extension  = FileTable["DBS_Extension"].TypeString();
                    }

                    FileTable.Clear();

                    if (AppCommon.PurviewCheck(Id, false, "downloader", ref Conn) == false)
                    {
                        continue;
                    }

                    StoragePath = Context.Server.MapPath("/storage/file/");

                    if (Folder == 1)
                    // 导出文件夹
                    {
                        ExportFolder(Id, StoragePath, PackageFolderPath, Context);
                    }
                    else
                    // 导出文件
                    {
                        FilePath = Base.Common.PathCombine(StoragePath, FolderPath.Substring(1), CodeId + Extension);

                        if (File.Exists(FilePath) == false)
                        {
                            continue;
                        }

                        File.WriteAllBytes(Base.Common.PathCombine(PackageFolderPath, Name + Extension), ReadFileBytes(FilePath, CodeId));

                        AppCommon.Log(Id, "file-download", ref Conn);
                    }
                }

                // 压缩文件夹
                SevenZipCompressor.SetLibraryPath(Context.Server.MapPath("/bin/7z64.dll"));

                Compressor = new SevenZipCompressor();

                Compressor.ArchiveFormat    = OutArchiveFormat.Zip;
                Compressor.CompressionLevel = CompressionLevel.High;
                Compressor.CompressDirectory(PackageFolderPath, PackageFilePath);

                Compressor = null;

                // 输出zip文件
                OutputZip(PackageFilePath, Context);
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                if (Base.Common.IsNothing(Compressor) == false)
                {
                    Compressor = null;
                }

                Directory.Delete(PackageFolderPath, true);

                File.Delete(PackageFilePath);

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 文件查看
        /// </summary>
        private void View(HttpContext Context)
        {
            Hashtable FileTable  = new Hashtable();
            Hashtable TaskTable  = new Hashtable();
            string    Method     = "";
            int       Id         = 0;
            string    FolderPath = "";
            string    CodeId     = "";
            string    Name       = "";
            string    Extension  = "";
            string    FilePath   = "";

            try
            {
                Method = Context.Request.RequestType;

                if (Base.Common.IsNumeric(Context.Request.QueryString["Id"]) == true)
                {
                    Id = Context.Request.QueryString["Id"].TypeInt();
                }
                else
                {
                    Context.Response.StatusCode = 500;
                    return;
                }

                CodeId = Context.Request.QueryString["CodeId"].TypeString();

                if (Base.Common.StringCheck(CodeId, @"^[\d]{8}-[\d]{6}-[\d]{7}-[\d]{3}$") == false)
                {
                    Context.Response.StatusCode = 500;
                    return;
                }

                if (Method == "HEAD")
                {
                    Base.Data.SqlDataToTable("Select DBS_FileId, DBS_Convert From DBS_File_Process Where DBS_FileId = " + Id + " And DBS_Convert = 1", ref Conn, ref TaskTable);

                    if (TaskTable["Exist"].TypeBool() == true)
                    {
                        Context.Response.StatusCode = 403;
                        return;
                    }

                    TaskTable.Clear();
                }

                Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension From DBS_File Where DBS_Folder = 0 And DBS_Id = " + Id + " And DBS_CodeId = '" + CodeId + "'", ref Conn, ref FileTable);

                if (FileTable["Exist"].TypeBool() == false)
                {
                    Context.Response.StatusCode = 404;
                    return;
                }
                else
                {
                    Name       = FileTable["DBS_Name"].TypeString();
                    Extension  = FileTable["DBS_Extension"].TypeString();
                    FolderPath = FileTable["DBS_FolderPath"].TypeString();
                }

                FileTable.Clear();

                FilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension);

                if (File.Exists(FilePath) == false)
                {
                    Context.Response.StatusCode = 404;
                    return;
                }

                if (Extension != ".pdf")
                {
                    if (File.Exists("" + FilePath + ".pdf") == true)
                    {
                        FilePath = "" + FilePath + ".pdf";
                    }
                }

                if (Extension != ".flv")
                {
                    if (File.Exists("" + FilePath + ".flv") == true)
                    {
                        FilePath = "" + FilePath + ".flv";
                    }
                }

                if (Method == "GET")
                {
                    int Viewed = Base.Data.SqlScalar("Select Count(*) From DBS_Log Where DBS_UserId = " + Context.Session["UserId"].TypeString() + " And DBS_Time > '" + DateTime.Now.ToShortDateString() + " 00:00:00' And DBS_FileId = " + Id, ref Conn);

                    if (Viewed == 0)
                    {
                        AppCommon.Log(Id, "file-view", ref Conn);
                    }

                    Output(CodeId, FilePath, Context);
                }
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Exemplo n.º 8
0
        /// <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();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 读取压缩文件数据返回json格式字符串
        /// </summary>
        private void DataToJson(HttpContext Context)
        {
            ArrayList    JsonList    = new ArrayList();
            MemoryStream MS          = default(MemoryStream);
            dynamic      Zip         = default(dynamic);
            string       ArchivePath = "";
            string       ArchiveSize = "";
            Hashtable    FileTable   = new Hashtable();
            int          Id          = 0;
            string       FolderPath  = "";
            string       CodeId      = "";
            string       Extension   = "";
            string       FilePath    = "";

            byte[] Bytes = {};
            int    Index = 0;

            try
            {
                if (Base.Common.IsNumeric(Context.Request.QueryString["Id"]) == true)
                {
                    Id = Context.Request.QueryString["Id"].TypeInt();
                }
                else
                {
                    return;
                }

                Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Extension, DBS_Recycle From DBS_File Where DBS_Folder = 0 And DBS_Recycle = 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();
                    Extension  = FileTable["DBS_Extension"].TypeString();
                }

                FileTable.Clear();

                if (Extension != ".7z" && Extension != ".rar" && Extension != ".zip")
                {
                    return;
                }

                if (AppCommon.PurviewCheck(Id, false, "viewer", ref Conn) == false)
                {
                    return;
                }

                FilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension);

                if (File.Exists(FilePath) == false)
                {
                    return;
                }

                Bytes = ReadFileBytes(FilePath, CodeId);

                if (Bytes.Length == 0)
                {
                    return;
                }

                MS = new MemoryStream(Bytes);

                SevenZipCompressor.SetLibraryPath(Context.Server.MapPath("/bin/7z64.dll"));

                Zip = new SevenZipExtractor(MS);

                for (Index = 0; Index < Zip.ArchiveFileData.Count; Index++)
                {
                    if (Context.Response.IsClientConnected == false)
                    {
                        return;
                    }

                    ArchivePath = Zip.ArchiveFileData[Index].FileName.ToString().Replace("\\", "/") + (Zip.ArchiveFileData[Index].IsDirectory == true ? "/" : "");
                    ArchiveSize = Zip.ArchiveFileData[Index].Size.ToString();

                    JsonList.Add("{'path':'" + Base.Common.JsonEscape(ArchivePath) + "','size':'" + ArchiveSize + "'}");
                }

                Zip = null;

                MS.Dispose();

                Context.Response.Write("[" + string.Join(",", JsonList.ToArray()) + "]");
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);
            }
            finally
            {
                if (Base.Common.IsNothing(MS) == false)
                {
                    MS.Dispose();
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }