9) Discover if Files have been Renamed. |
File renaming has a common sense approach and is easy when working with the mechanism. The crux is to know the original name that all renaming is based on (base name). A file deployment (that is NOT a copy) is renamed if its written name is different in any way to its original name (or remote name) which is simply the name it had on the client machine before it was uploaded. A copied file is termed renamed if its written name is different to its name when it was created. An arbitrary file that exists on the file system which is not a file deployment is termed renamed if its written name is different to its written name when the XloadFile instance representing it was created. The code below demonstrates these simple rules.
Consider a directory 'uploaded' that exists inside the web directory with a file already in it called test.txt; we then a upload a single file called test.txt from a client machine into this directory (so that a name clash occurs) and into memory (Please note in this case we are using the default file renaming strategy whereby files are renamed by appending a (1) to the file name if a naming clash occurs and incrementing the numeral by 1 on subsequent clashes):
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); xman.target("file1", 2048); xman.upload();
|
Now we see that after the initial upload a rename has taken place:
XloadFile diskFile = xman.getFile("file1", "uploaded"); //gets written file diskFile.isRenamed() //returns true
|
When we move this disk based file deployment it remains renamed as its base name still relates to its original remote name on the client machine:
diskFile.moveTo("uploaded/folder1"); diskFile.isRenamed() //still returns true
|
After copying the disk based file deployment, its base name, that renaming is based on, becomes its initial written name when the copy was created and therefore is now NOT termed renamed:
XloadFile diskCopy = diskFile.copyTo("uploaded"); diskCopy.isRenamed(); //returns false
|
We now reset the target name of the memory loaded file ready to move to another location. When moved using the new file name, the new XloadFile instance represents the memory load on disk and is therefore termed renamed as its name is now different to its base name which is its original remote name on the client machine (note that when a memory loaded file deployment is moved the memory based file deployment is destroyed and the new disk based file deployment created remembers the state of the memory file deployment just destroyed):
XloadFile memFile = xman.getFile("file1"); //gets memory file memFile.resetTargetName("newtest.txt"); XloadFile memFileOnDisk = memFile.moveTo("uploaded"); memFileOnDisk.isRenamed(); //returns true
|
An arbitrary disk file’s base name is the writtenName of the file when the XloadFile object was created (i.e. first line of the following code). So when moving, unless the target name is reset, the file name will be unchanged and renaming will be set to false:
XloadFile arbFile = xman.getAnyFile("uploaded", "arbFile"); arbFile.moveTo("uploaded/folder1"); arbFile.isRenamed(); //returns false
|
Now we reset the target name and copy to another location, but because it is a copy, the base name is set to the name when the copy was created and therefore renaming is set to false:
arbFile.resetTargetName("arbFile1"); XloadFile arbCopy = arbFile.copyTo("uploaded"); arbCopy.isRenamed(); //returns false
|
After directly renaming then renaming is set to true:
arbCopy.renameTo("arbFile2"); arbCopy.isRenamed(); //returns true
|
IMPORTANT:- Any files that are returned from the getAnyFile() method of the XloadManager class will be of the correct type (i.e. a file deployment or not) and will contain the correct state. Remember, file deployments only exist as file deployments for the life of the XloadManager object that they were created from. |
IMPORTANT:- Normally when the written name of a file is changed its target name changes to the same value. After resetTargetName() is called then the target name can only be altered by this method for the life of the object. |
It can be determined easily in a number of ways if any file deployments have been renamed or not. The code below demonstrates:
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); ...... ..... xman.upload(); if(xman.anyRenamed()){ List list = xman.getRenamed(); //deal with any renamed file deployments }
|
A much more fine grained approach could be as follows:
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); ...... ..... xman.upload(); XloadFileUpload upload1 = xman.getFileUpload("file1"); if(upload1.anyRenamed()){ List list = upload1.getRenamed(); }
|
The above methods return all file deployments that have been renamed including the ones that are copies of file deployments (which are also termed file deployments). The renamed files are returned as immutable lists that cannot be altered in any way and only read from.
At any time, you can determine what the remote name, target name or written name are by calling appropriate methods against any XloadFile object. This is demonstrated below:
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); ... ... xman.upload(); XloadFile file = xman.getFile("file1"); String targetName = file.getTargetName(); String writtenName = file.getWrittenName(); String remoteName = file.getRemoteName();
|
where:
request - HttpServletRequest object.
uploaded - Directory to upload files to (relative to the web application directory).
file1 - File parameter inside html (or other) form.
Please note that an ordinary file returned by the method getAnyFile(String) of the XloadManager class on the file system will return null for its remote name, as it is not a file deployment according to the XloadManager object that it was retrieved from.
© Gubutech(Xload) 2006 (v1.2)