Scan.inc
<%
'Explanation: This is my first time writing an application class. Please tell me if there is any inappropriateness! QQ: 1168064
'Properties and methods
'1. ScanType: The type of scan. Default value: 1. Values: 0 scans files and databases 1 scans files 2 scans databases.
'2. Conn, Table, ColImg, ColID: used when scanning the database, respectively the connection string, table name, picture column name, and the ID column name corresponding to the picture.
'3. List: display type. Default value: 0. Value: 0 invalid picture 1 network picture 2 valid picture 3 all
'4. ScanText: Scanned image type. Default value: Asp/html/htm. Value: File extension, separated by "/".
'5. Path: Scanned path: The default is the website root directory, please use relative paths. For example "/dsj"
'6. Scan(): method. Scan according to settings
'7. File: Save all scanned information. Called after the Scan() method
'8. Folders: Number of folders scanned
'9. Files: Number of scanned files.
'10. TotalSize: The total size of the directory. G, M, B are automatically displayed.
'11. Images: Number of pictures in the scanned file
'12. Exists: number of failures
'13. DbImg: Number of pictures in the database
'14. TotalImg: Number of scanned pictures
'15. RunTime: The time of the scanning process. Unit millisecond
'16. Regarding the use of File:
' For Each Fn In ObjName.file …… Next
'Fn.FileName: Picture name, including path
' Fn.Belong: the file or database where the picture is located (the files are separated by "|")
'Fn.Exists: whether it is valid. 0 means invalid, 1 means valid - 1 means non-local path and cannot be judged.
Option Explicit
ClassMCScanImg
dim File,ScanType,Conn,Table,ColId,ColImg,FSO,Path,List,ScanText,Spath,Version
dim Folders,Files,TotalSize,Images,Exists,sFiles,Start,EndT,RunTime,DbImg,TotalImg,Filter
Private Sub Class_Initialize
Set File = Server.Createobject("Scripting.Dictionary")
Set FSO = CreateObject("Scripting.FileSystemObject")
ScanType=1
Conn=""
Table=""
ColImg=""
ColId=""
Path="/"
sPath = Server.MapPath("/")
List=0
ScanText="asp/htm/html"
Folders=0
Files=0
TotalSize=0
Images=0
DbImg=0
Exists=0
sFiles=0
TotalImg=0
Start=Timer
Endt=Timer
Runtime=0
Filter="src=(.[^>^&]*)(.gif|.jpg)"
Version="1.00"
End Sub
Private Sub Class_Terminate
Set File=Nothing
Set FSO = Nothing
End Sub
Public Function Scan() 'Start scanning
if left(path,1)="/" then
path=Spath&Replace(path,"/","")
else
Path=Spath&""&Replace(path,"/","")
end if
If ScanType=1 then
Scanfile(Path)
ElseIf ScanType=2 Then
ScanDb()
Else
ScanFile(Path)
ScanDb()
End If
EndT=timer
RunTime=FormatNumber(EndT-Start)*1000
TotalSize=shb(TotalSize)
TotalImg=DbImg+Images
End Function
Private Sub ScanDB() 'Scan the database. The path here is difficult to determine, please change it in InsDb (after If AddNum=0)
Dim Rs,RetStr,ReBel,SQL
SQL="Select "&ColID&","&ColIMG&" From "&Table&" Order by "&ColID&" DESC"
'On Error Resume Next
If Conn ="" OR Table="" OR ColID="" OR ColIMG = "" Then
Exit Sub
Else
Set Rs = Server.CreateObject("ADODB.RecordSet")
Rs.Open SQL,conn,3,3
While Not Rs.EOF
RetStr=Rs(1)
ReBel="&ColImg&" column in table "&Table&" (ID: "&Rs(0)&")"
InsDb RetStr,ReBel,0,""
Rs.MoveNext
Wend
Rs.Close
Set Rs=Nothing
End If
End Sub
Private Sub ScanFile(PathStr) 'Scan the file. recursion
Dim f,ff,fn,fd,fdn,RealPath,fr,fc
'Response.write PathStr&"<br>"
Set ff = fso.getfolder(pathstr)
Set f = ff.files
Set fd = ff.subfolders
If f.Count >0 Then
For Each fn In f
Files=Files+1
TotalSize=TotalSize+fn.Size
If ChkFileName(fn.Name) Then
sFiles=sFiles+1
If Right(PathStr,1) <> "" Then
RealPath=PathStr&""&fn.Name
Else
RealPath=PathStr&fn.Name
End If
Set fr = FSO.OpenTextFile(RealPath,1)
fc=fr.ReadAll
'response.write RealPath&"<br>"
RegExpTest filter,fc,RealPath
End If
Next
End If
If fd.Count> 0 Then
For Each fdn In fd
Folders=Folders+1
dim temp
if right (PathStr,1) <> "" then
temp=PathStr&""&fdn.Name
else
temp=PathStr&fdn.Name
end if
ScanFile(temp)
Next
End If
End Sub
Private Sub RegExpTest(Patrn, Strng,PathStr) 'Find pictures
Dim RegEx, Match, Matches,Chk,ReImg,RetStr,ReBel,TheFile
Set RegEx = New RegExp
RegEx.Pattern = Patrn
RegEx.IgnoreCase = True
RegEx.Global = True
Set Matches = RegEx.Execute(Strng)
For Each Match in Matches
RetStr = Replace(Match.Value,"src=","")
RetStr = Replace(RetStr,"'","")
RetStr = Replace(RetStr,"""","")
Chk = 0
ReBel=GetFn(PathStr)
InsDb RetStr,ReBel,1,PathStr
Next
End Sub
Private Function GetExt(FullPath) 'Get the file extension, used to determine whether it is a scanned file type
Dim Temp
If FullPath <> "" Then
Temp = Mid(FullPath,InStrRev(FullPath, "")+1)
If InStr(Temp,".")>0 Then
GetExt=Mid(Temp,InStrRev(Temp, ".")+1)
Else
GetExt=Temp
End If
Else
GetExt = ""
End If
End Function
Private Function ChkFileName(Str) 'Detect whether the file is the file type to be scanned
Dim ar,i,fn
fn=GetExt(str)
ar=Split(ScanText,"/")
ChkFileName=False
For i=0 To ubound(ar)
If lCase(fn) =lCase(Trim(ar(i))) Then
ChkFileName=True
Exit Function
End If
Next
End Function
Private Function shb(n) 'Display the number of bytes
If n<1024 Then
shb = n&"byte"
ElseIf n>1024 and n<1024*1024 Then
shb = formatnumber(n/1024,2)&"K"
ElseIf n>=1024*1024 and n <1024*1024*1024 Then
shb = formatnumber(n/(1024*1024),2)&"M"
Else
shb =formatnumber(n/(1024*1024*1024),2)&"G"
End If
End Function
Private Sub InsDb(RetStr,ReBel,AddNum,PathStr) 'Analyze whether the picture is valid and add it to the dictionary object
dim chk,ReImg,TheFile
If InStr(RetStr," http://")>0 OR Instr(RetStr," ftp://")>0 Then
ReImg=RetStr
Chk=-1
Else
RetStr = Replace(RetStr,"/","")
If (Left(RetStr,1) = "" ) Then
RetStr=SPath&Retstr
ElseIf Left(RetStr,3) = ".." Then
dim temp
temp=GetPath(PathStr)
Do Until Left(RetStr,3) <> ".." 'Handle relative paths
Temp=Fso.GetParentFolderName(Temp)
RetStr=Mid(RetStr,4,len(RetStr)-3)
Loop
RetStr=Temp&""&RetStr
Else
If AddNum=0 Then
if left(RetStr,1)="" then
RetStr=Path&""&Retstr
Else
RetStr=path&Retstr
End If
else
RetStr=getpath(Pathstr)&RetStr
End IF
End If
If FSO.FileExists(RetStr) Then
Chk=1
End If
ReImg=GetFn(RetStr)
End If
If Chk=0 Then
Exists=Exists+1
End if
If File.Exists(ReImg) then
Set TheFile=File.Item(ReImg)
If TheFile.Belong <> ReBel Then
TheFile.Belong=TheFile.Belong&"|"&Rebel
End If
Else
If (List=0 AND Chk =0) OR (List=1 And Chk=-1) Or (List=2 And Chk=1 ) Or List=3 Then
Set TheFile= New FileInfo
TheFile.FileName=ReImg
TheFile.Belong=ReBel
TheFile.Exists=Chk
File.Add ReImg,TheFile
Select Case ScanType
Case 1 Images=Images+1
Case 2 DbImg = DbImg+1
Case Else
If AddNum = 0 Then
DbImg = DbImg+1
Else
Images=Images+1
End If
End Select
End If
End If
End Sub
Private Function GetPath(Str) 'Get the file path
'response.write str&"<br>"
Dim Temp,EndB
Temp=Replace(Str,"/","")
EndB=InstrRev(Temp,"")
If EndB = 0 Then
GetPath=SPath
Else
GetPath=Left(Temp,EndB)
End If
'response.write GetPath&"<BR>"
End Function
Private Function GetFn(Str) 'Get the relative path name of the file
Dim Temp
Temp=Str
'response.write temp&"<br>"
Temp=Replace(Str,SPath,"")
Temp=Replace(Temp,"","/")
GetFn=Temp
End Function
End Class
Class FileInfo
Dim FileName,Belong,Exists
Private Sub Class_Initialize
FileName=""
Belong=""
Exists=""
End sub
End Class
%>
Application examples
< %@LANGUAGE="VBSCRIPT " CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd ">
<%
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<form name="form1" method="post" action="scan.asp">
<table width="60%" border="0" align="center" cellspacing="1" bgcolor="#003366">
<tr bgcolor="#FFFFFF">
<td height="30" colspan="2" bgcolor="#00CCFF"><div align="center">Scan picture</div></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="26%" height="20"><div align="right">Scan folder:</div></td>
<td width="74%" height="20"><select name="Path" id="Path">
<option value="/">/</option>
<%
dim fso,f,fd,p
p=server.MapPath("/")
set fso=Server.CreateObject("Scripting.FileSystemObject")
function showpath(str)
set f=fso.getfolder(str)
set fd=f.subfolders
for each fds in fd
Response.Write "<option value="&Replace(Replace(fds,p,""),"","/")&">"&Replace(Replace(fds,p,""),""," /")&"</option>"
set ff=fso.getfolder(fds)
set ffd=ff.subfolders
if ffd.count>0 then
showpath(fds)
end if
next
end function
showpath(p)%>
</select></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="20"><div align="right">Scan type: </div></td>
<td height="20"><input type="radio" name="SType" value="0">
all
<input name="SType" type="radio" value="1" checked>
Scan files
<input type="radio" name="SType" value="2">
Scan database</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="20"><div align="right">Display type:</div></td>
<td height="20"><input name="LType" type="radio" value="0" checked>
Invalid
<input type="radio" name="LType" value="1">
network path
<input type="radio" name="LType" value="2">
efficient
<input type="radio" name="LType" value="3">
All</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="20"><div align="right">File type: </div></td>
<td height="20"><input name="Ext" type="checkbox" id="Ext" value="asp" checked>
Asp
<input name="Ext" type="checkbox" id="Ext" value="htm" checked>
Htm
<input name="Ext" type="checkbox" id="Ext" value="html" checked>
Html
<input name="Ext" type="checkbox" id="Ext" value="inc" checked>
Inc</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="20"><div align="right">Database:</div></td>
<td height="20">Table:
<input name="Tab" type="text" id="Tab" size="5" class="allinput">
Image ID column:
<input name="ColID" type="text" id="ColID" size="5" class="allinput">
Image path column:
<input name="ColImg" type="text" id="ColImg" size="5" class="allinput"> </td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="40" colspan="2"><div align="center">
<input type="submit" value="Start Scan" class="allinput">
</div></td>
</tr>
</table>
</form>
</body>
</html>
scan.asp
<!--#include file="scan.inc"-->
<%
dim mcs,fn,fb
%>
<link href="css.css" rel="stylesheet">
<table width="70%" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#003366">
<tr bgcolor="#AAAAFF">
<td width="30%" height="30">Picture name</td>
<td width="39%" height="30">Location</td>
<td width="31%" height="30">valid</td>
</tr>
<%
Function GetVar(ID,Default)
GetVar = Default
If Request(ID) <> "" Then
GetVar = Request(ID)
End IF
End Function
Dim SType,LType,Path,Ext,Conn,Tab,ColID,ColImg
SType=GetVar("SType",1)
LType=GetVar("LType",3)
Path=GetVar("Path","/")
Ext = Trim(Replace(GetVar("Ext","htm,html,asp,inc"),", ","/"))
Conn=GetVar("Conn","")
Tab=GetVar("Tab","")
ColID=GetVar("ColID","")
ColImg=GetVar("ColImg","")
Conn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("/db1.mdb")
set mcs= new mcscanimg
mcs.ScanType=SType
mcs.list=LType
mcs.ScanText=Ext
mcs.conn=Conn
mcs.Path=Path
mcs.table=Tab
mcs.ColID=ColID
mcs.ColImg=ColImg
mcs.scan()
for each fn in mcs.file
set fb=mcs.file(fn)
%>
<tr bgcolor="#FFFFFF">
<td valign="top"><%=fb.filename%></td>
<td><%=Replace(fb.Belong,"|","<br>")%></td>
<td><%
if fb.Exists=1 then
response.Write "valid path"
elseif fb.exists=0 then
response.Write "Invalid path"
else
response.Write "non-local path"
end if
%></td>
</tr>
<%
next
%>
<tr bgcolor="#FFFFFF">
<td colspan="3">Total scanned files: <%=mcs.files%>; Scanned folders: <%=mcs.folders%>; Total size: <%=mcs.totalsize%><br>Scanned pictures Number: <%=mcs.images&"; Number of database images: "&mcs.dbimg&"; Total number of images: "&mcs.TotalImg%>; Number of failures: <%=mcs.exists%><br>Running time :<%=mcs.runtime%>milliseconds</td>
</tr>
</table>
<%set mcs=nothing%>