Re: Storage provider and Upload provider

  •  10-04-2012, 11:11 PM

    Re: Storage provider and Upload provider

    Here is the code for uploader provider :

     

    1. using System;  
    2. using System.Data;  
    3. using System.Data.SqlClient;  
    4. using RTE;  
    5. namespace UploaderDatabaseProvider  
    6. {  
    7.     /// <summary>  
    8.     /// UploaderSqlServerProvider  
    9.     /// </summary>  
    10.     public class UploaderSqlServerProvider : RTE.UploaderProvider  
    11.     {  
    12.         const int BUFFERSIZE = 204800;  
    13.         SqlConnection _conn;  
    14.         public override void Init(IAjaxUploader uploader, System.Web.HttpContext context)  
    15.         {  
    16.             string connectionstring = System.Configuration.ConfigurationSettings.AppSettings["UploaderDatabase"];  
    17.             if (connectionstring == nullthrow (new Exception("appSettings:UploaderDatabase not found."));  
    18.             _conn = new SqlConnection(connectionstring);  
    19.             _conn.Open();  
    20.         }  
    21.         public override void Dispose()  
    22.         {  
    23.             if (_conn != null)  
    24.                 _conn.Close();  
    25.             base.Dispose();  
    26.         }  
    27.         private SqlCommand CreateCommand(string commandtext)  
    28.         {  
    29.             if (_conn == nullthrow (new Exception("Not init yet."));  
    30.             SqlCommand cmd = new SqlCommand();  
    31.             cmd.Connection = _conn;  
    32.             cmd.CommandText = commandtext;  
    33.             return cmd;  
    34.         }  
    35.         public override bool SupportFS  
    36.         {  
    37.             get  
    38.             {  
    39.                 return false;  
    40.             }  
    41.         }  
    42.         public override string GetFSPath(Guid guid)  
    43.         {  
    44.             throw (new NotSupportedException());  
    45.         }  
    46.         public override void Maintain()  
    47.         {  
    48.             SqlCommand cmd = CreateCommand("DELETE [AjaxUploaderTempFiles] WHERE FileTime<@Time");  
    49.             cmd.Parameters.Add("@Time", SqlDbType.DateTime).Value = DateTime.Now.AddHours(-1);  
    50.             cmd.ExecuteNonQuery();  
    51.         }  
    52.         public override bool GetInfo(Guid guid, out string filename, out int filesize, out bool persist)  
    53.         {  
    54.             SqlCommand cmd = CreateCommand("SELECT [FileName],[FileSize],[IsPersist] FROM [AjaxUploaderTempFiles] WHERE FileGuid=@Guid");  
    55.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    56.             using (SqlDataReader reader = cmd.ExecuteReader())  
    57.             {  
    58.                 if (reader.Read())  
    59.                 {  
    60.                     filename = reader.GetString(0);  
    61.                     filesize = reader.GetInt32(1);  
    62.                     persist = reader.GetBoolean(2);  
    63.                     return true;  
    64.                 }  
    65.                 else  
    66.                 {  
    67.                     filename = null;  
    68.                     filesize = 0;  
    69.                     persist = false;  
    70.                     return false;  
    71.                 }  
    72.             }  
    73.         }  
    74.         public override void Delete(Guid guid)  
    75.         {  
    76.             SqlCommand cmd = CreateCommand("DELETE [AjaxUploaderTempFiles] WHERE FileGuid=@Guid");  
    77.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    78.             cmd.ExecuteNonQuery();  
    79.         }  
    80.         public override void Persist(Guid guid)  
    81.         {  
    82.             SqlCommand cmd = CreateCommand("UPDATE [AjaxUploaderTempFiles] SET IsPersist=1 WHERE FileGuid=@Guid");  
    83.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    84.             cmd.ExecuteNonQuery();  
    85.         }  
    86.         public override void UnPersist(Guid guid)  
    87.         {  
    88.             SqlCommand cmd = CreateCommand("UPDATE [AjaxUploaderTempFiles] SET IsPersist=0 WHERE FileGuid=@Guid");  
    89.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    90.             cmd.ExecuteNonQuery();  
    91.         }  
    92.         public override void Save(Guid guid, string filename, System.IO.Stream stream)  
    93.         {  
    94.             int stepsize = BUFFERSIZE;  
    95.             long filesize = stream.Length;  
    96.             byte[] data = new byte[Math.Min(stepsize, filesize)];  
    97.             stream.Read(data, 0, data.Length);  
    98.             SqlCommand cmd = CreateCommand("INSERT INTO [AjaxUploaderTempFiles] ([FileGuid],[FileTime],[FileName],[FileSize],[FileData],[IsPersist]) VALUES (@Guid,@Time,@Name,@Size,@Data,0)");  
    99.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    100.             cmd.Parameters.Add("@Time", SqlDbType.DateTime).Value = DateTime.Now;//for Maintain  
    101.             cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255).Value = filename;  
    102.             cmd.Parameters.Add("@Size", SqlDbType.Int).Value = filesize;  
    103.             cmd.Parameters.Add("@Data", SqlDbType.Image).Value = data;  
    104.             cmd.ExecuteNonQuery();  
    105.             if (filesize <= stepsize)  
    106.                 return;  
    107.             int sentsize = stepsize;  
    108.             try  
    109.             {  
    110.                 while (true)  
    111.                 {  
    112.                     int readsize = stream.Read(data, 0, data.Length);  
    113.                     if (readsize <= 0)  
    114.                         return;  
    115.                     cmd = CreateCommand("DECLARE @ptrval binary(16) ; SELECT @ptrval = TEXTPTR([FileData]) FROM [AjaxUploaderTempFiles] WHERE [FileGuid]=@Guid ; UPDATETEXT [AjaxUploaderTempFiles].[FileData] @ptrval "  
    116.                     + sentsize + " 0 @Data");  
    117.                     cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    118.                     if (readsize != data.Length)  
    119.                     {  
    120.                         byte[] newdata = new byte[readsize];  
    121.                         Buffer.BlockCopy(data, 0, newdata, 0, readsize);  
    122.                         data = newdata;  
    123.                     }  
    124.                     cmd.Parameters.Add("@Data", SqlDbType.Image).Value = data;  
    125.                     cmd.ExecuteNonQuery();  
    126.                     sentsize += readsize;  
    127.                 }  
    128.             }  
    129.             catch (Exception)  
    130.             {  
    131.                 Delete(guid);  
    132.                 throw;  
    133.             }  
    134.         }  
    135.         public override void AppendData(Guid guid, string filename, System.IO.Stream stream)  
    136.         {  
    137.             int stepsize = BUFFERSIZE;  
    138.             long filesize = stream.Length;  
    139.             byte[] data = new byte[stepsize];  
    140.             SqlCommand cmd = CreateCommand("SELECT [FileSize] FROM [AjaxUploaderTempFiles] WHERE [FileGuid]=@Guid ; UPDATE [AjaxUploaderTempFiles] SET [FileSize]=[FileSize]+@Size WHERE [FileGuid]=@Guid");  
    141.             cmd.Parameters.Add("@Size", SqlDbType.Int).Value = filesize;  
    142.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    143.             int sentsize = Convert.ToInt32(cmd.ExecuteScalar());  
    144.             try  
    145.             {  
    146.                 while (true)  
    147.                 {  
    148.                     int readsize = stream.Read(data, 0, data.Length);  
    149.                     if (readsize <= 0)  
    150.                         break;  
    151.                     cmd = CreateCommand("DECLARE @ptrval binary(16) ; SELECT @ptrval = TEXTPTR([FileData]) FROM [AjaxUploaderTempFiles] WHERE [FileGuid]=@Guid ; UPDATETEXT [AjaxUploaderTempFiles].[FileData] @ptrval "  
    152.                     + sentsize + " 0 @Data");  
    153.                     cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    154.                     if (readsize != data.Length)  
    155.                     {  
    156.                         byte[] newdata = new byte[readsize];  
    157.                         Buffer.BlockCopy(data, 0, newdata, 0, readsize);  
    158.                         data = newdata;  
    159.                     }  
    160.                     cmd.Parameters.Add("@Data", SqlDbType.Image).Value = data;  
    161.                     cmd.ExecuteNonQuery();  
    162.                     sentsize += readsize;  
    163.                 }  
    164.             }  
    165.             catch (Exception)  
    166.             {  
    167.                 Delete(guid);  
    168.                 throw;  
    169.             }  
    170.         }  
    171.         public override System.IO.Stream OpenStream(Guid guid)  
    172.         {  
    173.             SqlCommand cmd = CreateCommand("SELECT DATALENGTH([FileData]) FROM [AjaxUploaderTempFiles] WHERE FileGuid=@Guid");  
    174.             cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    175.             object val = cmd.ExecuteScalar();  
    176.             if (val == null || Convert.IsDBNull(val))  
    177.                 throw (new Exception("File not found."));  
    178.             int filesize = Convert.ToInt32(val);  
    179.             int stepsize = BUFFERSIZE;  
    180.             if (filesize <= stepsize)  
    181.             {  
    182.                 cmd = CreateCommand("SELECT [FileData] FROM [AjaxUploaderTempFiles] WHERE FileGuid=@Guid");  
    183.                 cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    184.                 using (SqlDataReader reader = cmd.ExecuteReader())  
    185.                 {  
    186.                     if (reader.Read())  
    187.                     {  
    188.                         byte[] data = (byte[])reader.GetValue(0);  
    189.                         return new System.IO.MemoryStream(data);  
    190.                     }  
    191.                 }  
    192.                 throw (new Exception("File not found."));  
    193.             }  
    194.             ReadStream readstream = new ReadStream();  
    195.             readstream.provider = this;  
    196.             readstream.guid = guid;  
    197.             readstream.filesize = filesize;  
    198.             return readstream;  
    199.         }  
    200.         class ReadStream : System.IO.Stream  
    201.         {  
    202.             public UploaderSqlServerProvider provider;  
    203.             public Guid guid;  
    204.             public int filesize;  
    205.             long pos = 0;  
    206.             byte[] _tempbuff = null;  
    207.             long _tempstart = -1;  
    208.             public override long Length  
    209.             {  
    210.                 get  
    211.                 {  
    212.                     return filesize;  
    213.                 }  
    214.             }  
    215.             public override long Position  
    216.             {  
    217.                 get  
    218.                 {  
    219.                     return pos;  
    220.                 }  
    221.                 set  
    222.                 {  
    223.                     if (value < 0) throw (new ArgumentOutOfRangeException("Position"));  
    224.                     if (value >= filesize) throw (new ArgumentOutOfRangeException("Position"));  
    225.                     pos = value;  
    226.                     _tempbuff = null;  
    227.                     _tempstart = -1;  
    228.                 }  
    229.             }  
    230.             public override int Read(byte[] buffer, int offset, int count)  
    231.             {  
    232.                 int readsize = 0;  
    233.                 while (true)  
    234.                 {  
    235.                     if (_tempstart != -1 && _tempbuff != null)  
    236.                     {  
    237.                         int start = (int)(pos - _tempstart);  
    238.                         if (start >= 0 && start < _tempbuff.Length)  
    239.                         {  
    240.                             int copysize = Math.Min(count, _tempbuff.Length - start);  
    241.                             Buffer.BlockCopy(_tempbuff, start, buffer, offset, copysize);  
    242.                             pos += copysize;  
    243.                             readsize += copysize;  
    244.                             offset += copysize;  
    245.                             count -= copysize;  
    246.                             if (count <= 0)  
    247.                                 return readsize;  
    248.                         }  
    249.                     }  
    250.                     if (pos >= filesize)  
    251.                         return readsize;  
    252.                     using (SqlCommand cmd = provider.CreateCommand("DECLARE @ptrval binary(16) ; SELECT @ptrval = TEXTPTR([FileData]) FROM [AjaxUploaderTempFiles] WHERE [FileGuid]=@Guid ; READTEXT [AjaxUploaderTempFiles].[FileData] @ptrval "  
    253.                     + pos + " " + Math.Min(BUFFERSIZE, filesize - pos)))  
    254.                     {  
    255.                         cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;  
    256.                         _tempbuff = (byte[])cmd.ExecuteScalar();  
    257.                         _tempstart = pos;  
    258.                     }  
    259.                 }  
    260.                 //return readsize;  
    261.             }  
    262.             public override bool CanRead  
    263.             {  
    264.                 get  
    265.                 {  
    266.                     return true;  
    267.                 }  
    268.             }  
    269.             public override bool CanWrite  
    270.             {  
    271.                 get  
    272.                 {  
    273.                     return false;  
    274.                 }  
    275.             }  
    276.             public override bool CanSeek  
    277.             {  
    278.                 get  
    279.                 {  
    280.                     return true;  
    281.                 }  
    282.             }  
    283.             public override void Close()  
    284.             {  
    285.             }  
    286.             public override void Flush()  
    287.             {  
    288.             }  
    289.             public override long Seek(long offset, System.IO.SeekOrigin origin)  
    290.             {  
    291.                 long oldpos = pos;  
    292.                 if (origin == System.IO.SeekOrigin.Begin)  
    293.                 {  
    294.                     Position = offset;  
    295.                 }  
    296.                 if (origin == System.IO.SeekOrigin.Current)  
    297.                 {  
    298.                     Position += offset;  
    299.                 }  
    300.                 if (origin == System.IO.SeekOrigin.End)  
    301.                 {  
    302.                     Position = filesize + offset;  
    303.                 }  
    304.                 return oldpos;  
    305.             }  
    306.             public override void SetLength(long value)  
    307.             {  
    308.                 throw (new NotSupportedException());  
    309.             }  
    310.             public override void Write(byte[] buffer, int offset, int count)  
    311.             {  
    312.                 throw (new NotSupportedException());  
    313.             }  
    314.         }  
    315.     }  
    316. }  
     

     

    1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AjaxUploaderTempFiles]'and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
    2. drop table [dbo].[AjaxUploaderTempFiles]  
    3. GO  
    4. CREATE TABLE [dbo].[AjaxUploaderTempFiles] (  
    5.  [FileGuid] [uniqueidentifier] NOT NULL ,  
    6.  [FileTime] [datetime] NOT NULL ,  
    7.  [FileName] [nvarchar] (255),  
    8.  [FileSize] [intNOT NULL ,  
    9.  [FileData] [image] NOT NULL ,  
    10.  [IsPersist] [bitNOT NULL   
    11. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
    12. GO  
    13. ALTER TABLE [dbo].[AjaxUploaderTempFiles] WITH NOCHECK ADD   
    14.  CONSTRAINT [PK_AjaxUploaderTempFiles] PRIMARY KEY  CLUSTERED   
    15.  (  
    16.   [FileGuid]  
    17.  )  ON [PRIMARY]   
    18. GO  

     

    1. <?xml version="1.0"?>  
    2. <configuration>  
    3.     <appSettings>  
    4.   
    5.         <add key="RTE.ImageEditor.TempFolder" value="~/rtetemp"/>  
    6.   
    7.         <add key="UploaderDatabase" value="server=(local);database=rtedb;uid=test;pwd=test"/>  
    8.         <add key="RTE.AjaxUploader.Provider" value="UploaderDatabaseProvider.UploaderSqlServerProvider,App_Code"/>  
    9.           
    10.     </appSettings>  
    11.     <connectionStrings/>  
    12.     <system.web>  
    13.         <customErrors mode="Off" />  
    14.         <pages validateRequest="true" enableViewStateMac="true" enableEventValidation="false" viewStateEncryptionMode="Always"   />  
    15.         <compilation debug="true">  
    16.             <assemblies>  
    17.                 <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>  
    18.         <authentication mode="None"/>  
    19.         <httpModules>  
    20.             <add name="UploadModule" type="RTE.UploadModule,RichTextEditor"/>  
    21.         </httpModules>  
    22.     </system.web>  
    23. </configuration>  

     

    Regards,
    Terry

     

View Complete Thread