/
ivan
/
ModMas
Обзор
Документация
Войти
/
ivan
/
ModMas
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ModuleMaster/dbmaint.aspx.vb
238 строк
9 KB
i.krivokon
Initial commit
13 мар 2026, 13:49
13 мар 2026, 13:49
00d9fb6
Код
Авторство
О чём код?
Imports sharedClasses.dbFunctions Imports System.Data Imports System.Net Imports System.IO Imports Newtonsoft.Json Imports sharedClasses Imports System.Math Imports System Imports System.Web Imports System.Text Imports System.Threading Imports Microsoft.VisualBasic Imports System.Drawing Imports System.Drawing.Drawing2D Partial Class dbmaint Inherits System.Web.UI.Page 'Dim runningRequestCount As Integer = 0 Dim maxRequestCount As Integer = 4 Private threadsToStart As Integer = maxRequestCount Dim maxitemstoprocess As Integer = 100000 Private idToProcess As New Collection Dim waitCounter As Integer = 0 'Public Shared allDone As New ManualResetEvent(False) Protected Sub goresize_Click(sender As Object, e As EventArgs) Handles goresize.Click Dim startTime As DateTime = DateTime.Now Dim endTime As DateTime resizelog.Text = "START: " & startTime.ToString If IsNumeric(threadsnumber.Text) Then maxRequestCount = threadsnumber.Text threadsToStart = threadsnumber.Text End If If IsNumeric(itemstoprocess.Text) Then maxitemstoprocess = itemstoprocess.Text End If Dim sqlf As New SqlFunctions sqlf.NonQuery("DELETE FROM UserModulesAttachments.[user]." & tablename.Text & " WHERE DELETED = 'Y' ") Dim sql As New StringBuilder sql.AppendLine("SELECT TOP " & maxitemstoprocess & " id FROM UserModulesAttachments.[user]." & tablename.Text) sql.AppendLine(" WHERE DELETED = 'N'") If chkOnlyNotResized.Checked Then sql.AppendLine("AND RESIZED = 'N' ") End If sql.AppendLine("AND (NAME LIKE '%.jpg' OR NAME LIKE '%.png') ORDER BY ID DESC ") Dim imagelist As DataTable = sqlf.GetData(sql.ToString) For Each dr As DataRow In imagelist.Rows idToProcess.Add(dr("id"), dr("id")) 'resizelog.Text &= vbNewLine & dr("id") Next processId() endTime = DateTime.Now resizelog.Text &= vbNewLine & " END:" & endTime.ToString resizelog.Text &= "DURATION: " & endTime.Subtract(startTime).TotalSeconds End Sub Protected Class imgProp Public tableName As String Public rowId As Integer Public Sub New(tb As String, id As Integer) tableName = tb rowId = id End Sub End Class Private Sub processId() Dim itemscounter As Integer = 0 While (idToProcess.Count > 0) And (itemscounter < maxitemstoprocess) If threadsToStart > 0 Then Dim id As Integer = idToProcess.Item(1) idToProcess.Remove(id.ToString) threadsToStart -= 1 Dim st As New imgProp(tablename.Text, id) ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf startThread), st) itemscounter += 1 End If End While 'While threadsToStart < maxRequestCount 'End While End Sub 'Private Sub startThread(id As Integer) ' Dim wb As WebRequest ' Dim url As String = HttpContext. ' Current. ' Request. ' Url. ' AbsoluteUri. ' ToString. ' Replace("test.aspx", ' "/handlers/getimage.ashx?mode=resize&id=" & id & "&storage=UserModulesAttachments.[user]." & tablename.Text) ' wb = WebRequest.Create(url) ' Dim myRequestState As New Object ' Dim asyncResult As IAsyncResult = CType(wb.BeginGetResponse(AddressOf endRequest, myRequestState), IAsyncResult) ' allDone.WaitOne() 'End Sub 'Private Sub endRequest(result As IAsyncResult) ' threadsToStart += 1 ' allDone.Set() 'End Sub Dim lasterror As String = "" Private Event allDone(imgid As Integer) Private Sub threadEnds(imgid As Integer) Handles Me.allDone threadsToStart += 1 End Sub Private Sub startThread(state As Object) Dim storagename As String = CType(state, imgProp).tableName Dim imageId As Integer = CType(state, imgProp).rowId readResizeSave(storagename, imageId) RaiseEvent allDone(imageId) End Sub Private Sub readResizeSave(storagename As String, imageId As Integer) Dim con As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("SERKIntranetConnectionString").ConnectionString) Dim com As New SqlClient.SqlCommand("", con) storagename = "UserModulesAttachments.[user]." & storagename com.CommandText = "SELECT [doc], [name] FROM " & storagename & " WHERE [id]=" & imageId Dim img() As Byte = Nothing Dim fName As String = "" Dim fExtension As String = "" Try con.Open() Dim r As SqlClient.SqlDataReader = com.ExecuteReader While r.Read img = r.Item("doc") fName = r.Item("name") End While Catch ex As Exception lasterror = ex.Message Finally If con.State <> Data.ConnectionState.Closed Then con.Close() End If End Try Dim c As New sharedClasses.Common.common Try 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 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, imageId, img, "resize") c.Log(storagename & "/thr:" & maxRequestCount, "resize", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "success: id - " & imageId & " osize - " & owidth & "x" & oheight) Else c.Log(storagename & "/thr:" & maxRequestCount, "resized already", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "skip: id - " & imageId & " osize - " & owidth & "x" & oheight) Dim sqlf As New SqlFunctions sqlf.NonQuery("UPDATE " & storagename & " SET RESIZED = 'Y' WHERE [ID] = " & imageId) End If End Using Catch ex As Exception c.Log(storagename & "/thr:" & maxRequestCount, "resize", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "fail: id - " & imageId & " ex - " & ex.Message) End Try End Sub 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 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 " 'com.CommandText = "UPDATE " & tablename & " SET doc = @thumb, resized = 'Y' 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 Protected Sub itemstoprocess_TextChanged(sender As Object, e As EventArgs) Handles itemstoprocess.TextChanged End Sub End Class