Continuing On...
October 9, 2000
Now that the user can view the hierarchy, we must allow them to
check files in and out. Assuming you've used the form from the
previous page, let's examine the code that will handle the
submission.
<%
dim intFileID, strFileName, intCount, strFunction, strLocation, i
dim strSQL, rst, strConnectionString
dim fso, f
'Set database variables
strConnectionString = "DSN=MySource;UID=username;PWD=password;Database=MySourceDB"
set rst = Server.CreateObject("ADODB.Recordset")
intCount = Request.form("file").Count
strFunction = Request.form("function") <-- What did the user
want to do? (check in, check out, etc)
for i = 1 to intCount
intFileID = Request.form("file")(i) <-- Grab the file
in question
strSQL = "SELECT Name, Location FROM tblFiles WHERE FID = " & intFileID
rst.open strSQL, strConnectionString
strFileName = rst(0)
strLocation = rst(1)
rst.close
'Use the filesystemobject to get the file in question
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(strLocation)
If (f.attributes and 1) AND strFunction = "Checkout" Then <-- If file is read only,
then change it to writeable
f.attributes = f.attributes - 1
Elseif NOT (f.attributes AND 1) and strFunction = "CheckIn" then <-- Otherwise,
make the file read only
f.attributes = f.attributes + 1
End If
f.close
set fso = nothing
'Update the database to show which user currently has the file checked out
strSQL = "SELECT Update SET User = '" & Session("Username") & "' WHERE " & _
"WHERE FID = " & intFileID
rst.open strSQL, strConnectionString
next
%>
Let's take a look at the above code. The first thing we do is
to loop through all the files the user checked in the previous step.
Each item in Request.Form("file") holds the FID that we'll need
to grab each file name and location from the
database. We then use the file system object (check out
a previous article
for a tutorial on the FSO) to open each file, and clear its
read-only flag, if it isn't cleared already. This is so the user
can download a writeable copy of the file, and also upload and
overwrite the old file. Finally, we update the database with the
proper user information. Pretty simple, huh?
Let's take a step back for a second. Imagine that a user checks out
a file, and then downloads it. The application will clear the
read-only flag from the file on the server. Now, any other user
can write over that file as well! This is a serious drawback in many
source control systems today, even in Visual Source Safe. For
a web centric application, one solution would be to monitor the download -
once the file is downloaded, reset the read-only flag. That way,
you could easily build a function to allow only the user who
checked the file out to overwrite
the file on the web server. Obviously, we can't do this with FTP, so
let's take a look at another method.
The Front End
Effective Source Control
Getting Files
|