/
ivan
/
ModMas
Обзор
Документация
Войти
/
ivan
/
ModMas
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ModuleMaster/handlers/getimage.ashx
165 строк
7 KB
i.krivokon
Initial commit
13 мар 2026, 13:49
13 мар 2026, 13:49
00d9fb6
Код
Авторство
О чём код?
<%@ WebHandler Language="VB" Class="getFile" %> Imports System.Data.SqlClient Imports System.Drawing Imports System.Drawing.Drawing2D Public Class getFile : Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim rowid = context.Request.QueryString("id") Dim mode As String = IIf(IsNothing(context.Request.QueryString("mode")), "full", context.Request.QueryString("mode")) Dim message As New StringBuilder If IsNumeric(rowid) Then Dim storageName As String = context.Request.QueryString("storage") If IsNothing(storageName) Then storageName = "documents" End If Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SERKIntranetConnectionString").ConnectionString) Dim com As New SqlCommand("", con) Dim sqlf As New sharedClasses.dbFunctions.SqlFunctions Dim noThumb As Boolean = False If mode = "thumb" Then noThumb = (sqlf.GetScalar("SELECT COUNT(*) FROM " & storageName & " WHERE [thumb] is not NULL and [id]=" & rowid) = "0") If noThumb Then com.CommandText = "SELECT [doc], [name] FROM " & storageName & " WHERE [id]=" & rowid Else com.CommandText = "SELECT [thumb] as [doc], [name] FROM " & storageName & " WHERE [id]=" & rowid End If Else com.CommandText = "SELECT parentid, [doc], [name] FROM " & storageName & " WHERE [id]=" & rowid End If Dim img() As Byte = Nothing Dim fName As String = "" Dim fExtension As String = "" Dim parentID As String = "" Try con.Open() Dim r As SqlDataReader = com.ExecuteReader While r.Read img = r.Item("doc") fName = r.Item("name") parentID = r.Item("parentid") End While Catch ex As Exception Finally If con.State <> Data.ConnectionState.Closed Then con.Close() End If End Try Try If mode = "thumb" And noThumb Then Using imgMemoryStream As New IO.MemoryStream(img) Dim originalImage As Image = Image.FromStream(imgMemoryStream) originalImage = ResizeImage(originalImage, New Size With {.Width = 320, .Height = 240}) Using stream As New System.IO.MemoryStream originalImage.Save(stream, Imaging.ImageFormat.Jpeg) img = stream.ToArray End Using End Using SaveThumbToDb(storageName, rowid, img) ElseIf mode = "resize" Then Using imgMemoryStream As New IO.MemoryStream(img) Dim originalImage As Image = Image.FromStream(imgMemoryStream) Dim owidth As Integer = originalImage.Width Dim oheight As Integer = originalImage.Height If originalImage.Width > 1920 Or originalImage.Height > 1080 Then Try originalImage = ResizeImage(originalImage, New Size With {.Width = 1920, .Height = 1080}) Using stream As New System.IO.MemoryStream originalImage.Save(stream, Imaging.ImageFormat.Jpeg) img = stream.ToArray End Using SaveThumbToDb(storageName, rowid, img, "resize") message.AppendLine(rowid & " resized from " & owidth & "x" & oheight & " to 1920x1080 / " & lastError) Catch ex As Exception message.AppendLine("error (rowid " & rowid & " ): " & ex.Message) End Try Else message.AppendLine("no resize " & owidth & "x" & oheight) End If End Using End If Catch ex As Exception context.Response.ContentType = "text/html" context.Response.Write("Error: " & ex.Message) End Try Try If mode = "thumb" Then context.Response.ContentType = "image/jpeg" context.Response.AppendHeader("Content-disposition", "attachment;filename=" & fName & ";") context.Response.AppendHeader("Content-Length", img.Length.ToString()) context.Response.Cache.SetCacheability(HttpCacheability.Public) context.Response.BufferOutput = False context.Response.OutputStream.Write(img, 0, img.Length) ElseIf mode = "resize" Then context.Response.ContentType = "text" context.Response.Write(message) End If Catch ex As Exception End Try End If End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image Dim newWidth As Integer Dim newHeight As Integer If preserveAspectRatio Then Dim originalWidth As Integer = image.Width Dim originalHeight As Integer = image.Height Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth) Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight) Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth) newWidth = CInt(originalWidth * percent) newHeight = CInt(originalHeight * percent) Else newWidth = size.Width newHeight = size.Height End If Dim newImage As Image = New Bitmap(newWidth, newHeight) Using graphicsHandle As Graphics = Graphics.FromImage(newImage) graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight) End Using Return newImage End Function Dim lastError As String = "" Private Sub SaveThumbToDb(tablename As String, rowid As String, ByVal thumb() As Byte, Optional mode As String = "thumb") 'Saving files to DB Dim sqlf As New sharedClasses.dbFunctions.SqlFunctions Dim com As New Data.SqlClient.SqlCommand If mode = "resize" Then com.CommandText = "UPDATE " & tablename & " SET doc = @thumb where id = @rowid " Else com.CommandText = "UPDATE " & tablename & " SET thumb = @thumb where id = @rowid " End If com.Parameters.Clear() With com.Parameters .AddWithValue("thumb", thumb) .AddWithValue("rowid", rowid) End With sqlf.NonQuery(com) lastError = sqlf.lastException End Sub End Class