Author: xlawrence
Date: Tue May 15 16:31:09 2007
New Revision: 17313

URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17313&repname=
=3Djahia
Log:
Added method for form generator in order to allow file uploads, with automa=
tic creation of directories and automatic renaming of files if already exis=
ting

Modified:
    branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/engines/filemanage=
r/DAVFilemanager_Engine.java

Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/engines/file=
manager/DAVFilemanager_Engine.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/engines/filemanager/DAVFilemanager_Engine.j=
ava&rev=3D17313&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/engines/filemanage=
r/DAVFilemanager_Engine.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/engines/filemanage=
r/DAVFilemanager_Engine.java Tue May 15 16:31:09 2007
@@ -43,10 +43,7 @@
 import org.jahia.registries.ServicesRegistry;
 import org.jahia.services.acl.JahiaBaseACL;
 import org.jahia.services.categories.Category;
-import org.jahia.services.usermanager.JahiaGroup;
-import org.jahia.services.usermanager.JahiaGroupManagerService;
-import org.jahia.services.usermanager.JahiaUser;
-import org.jahia.services.usermanager.JahiaUserManagerService;
+import org.jahia.services.usermanager.*;
 import org.jahia.services.version.EntryLoadRequest;
 import org.jahia.services.webdav.DAVFileAccess;
 import org.jahia.services.webdav.JahiaWebdavBaseService;
@@ -56,6 +53,7 @@
 =

 import java.io.*;
 import java.text.Collator;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
@@ -945,7 +943,7 @@
             FileUpload fupload =3D paramBean.getFileUpload();
 =

             // get files name
-           final Set filesName =3D fupload.getFileNames();
+            final Set filesName =3D fupload.getFileNames();
 =

             for (Iterator iterator =3D filesName.iterator(); iterator.hasN=
ext();) {
                 String name =3D (String) iterator.next();
@@ -1068,12 +1066,131 @@
         return dir;
     }
 =

+    /**
+     * Method that uploads a file into the DAV repository and renames the =
file if it already exists, by adding a
+     * '_Number' at the end of the fileName. <br/>
+     * It is used by the form generator to upload files.<br/>
+     * It also puts the file inside a sub directory named as the current d=
ate.
+     *
+     * @param uploadPath The path where to upload the file
+     * @param processingContext
+     * @param fieldFormID The value of the 'name' attribute of the "input"=
 tag where the file was uploaded
+     * @return The download URL of the file or null if upload failed
+     */
+    public String uploadAndRenameAutomatically(final String uploadPath,
+                                               final ProcessingContext pro=
cessingContext,
+                                               final String fieldFormID) {
+        final ParamBean paramBean =3D (ParamBean) processingContext;
+        if (ProcessingContext.isMultipartRequest(paramBean.getRequest())) {
+            final FileUpload fupload =3D paramBean.getFileUpload();
+
+            // Let us first define the upload directory consisting of the =
upload path and the date of the day
+            final Date now =3D new Date();
+            final SimpleDateFormat sdf =3D new SimpleDateFormat("dd.MM.yyy=
y");
+            final String today =3D sdf.format(now);
+            final String path =3D uploadPath + "/" + today;
+            final JahiaUser adminUser =3D JahiaAdminUser.getAdminUser(proc=
essingContext.getSiteID());
+
+            logger.debug("uploadAndRenameAutomatically started: " + path +=
 ", fieldFormID: " + fieldFormID);
+
+            // Here is the final upload directory
+            DAVFileAccess uploadDir =3D jahiaWebdavBaseService.getDAVFileA=
ccess(processingContext,
+                    processingContext.getSite(), adminUser, path);
+
+            // If the directory does not exist, we will have to create it,=
 and maybe other ones as well, as parent
+            // directories might have to be created too
+            if (! uploadDir.isValid()) {
+                final StringTokenizer tokenizer =3D new StringTokenizer(pa=
th, "/");
+                final StringBuffer buff =3D new StringBuffer().append("/");
+
+                DAVFileAccess parent =3D jahiaWebdavBaseService.getDAVFile=
Access(processingContext,
+                        processingContext.getSite(), adminUser, buff.toStr=
ing());
+                // For each of the directories specified in the uploadPath=
, check that it exists
+                while (tokenizer.hasMoreTokens()) {
+                    final String token =3D tokenizer.nextToken();
+                    buff.append(token);
+                    final DAVFileAccess tmp =3D jahiaWebdavBaseService.get=
DAVFileAccess(processingContext,
+                            processingContext.getSite(), adminUser, buff.t=
oString());
+                    if (! tmp.isValid()) {
+                        parent.beginTransaction();
+                        final String result =3D parent.createCollection(to=
ken);
+                        logger.debug("Creating dir: " + token + " -> " + r=
esult);
+                        parent.commitTransaction();
+                        parent =3D jahiaWebdavBaseService.getDAVFileAccess=
(processingContext,
+                                processingContext.getSite(), adminUser, re=
sult);
+                    } else {
+                        parent =3D tmp;
+                    }
+                    buff.append("/");
+                }
+                uploadDir =3D jahiaWebdavBaseService.getDAVFileAccess(proc=
essingContext, processingContext.getSite(),
+                        adminUser, path);
+            }
+
+            logger.debug("Should be valid now: " + uploadDir.isValid());
+
+            // get file names
+            final Set filesNames =3D fupload.getFileNames();
+
+            final Iterator iterator =3D filesNames.iterator();
+            // For each of the files, upload it and rename it if there is =
an existing file
+            while (iterator.hasNext()) {
+                final String name =3D (String) iterator.next();
+                final String formField =3D fupload.getFormFieldName(name);
+                if (fupload.getFileSystemName(name) =3D=3D null || ! formF=
ield.equals(fieldFormID)) {
+                    continue;
+                }
+                String fileName =3D decodeStrangeBrowserEncoding(fupload.g=
etFileSystemName(name));
+                fileName =3D jahiaWebdavBaseService.cleanTitle(fileName);
+                if (Jahia.getSettings().isTransformFilenames()) {
+                    fileName =3D DAVFileAccess.cleanName(fileName);
+                }
+
+                DAVFileAccess old =3D jahiaWebdavBaseService.getDAVFileAcc=
ess(processingContext,
+                        processingContext.getSite(), adminUser, path + "/"=
 + fileName);
+
+                if (old.isValid()) { // File already exists -> rename it !
+                    int i =3D 1;
+                    final int index =3D fileName.indexOf(".");
+                    final String extension;
+                    final String theName;
+                    if (index > -1) {
+                        extension =3D fileName.substring(index);
+                        theName =3D fileName.substring(0, index);
+                    } else {
+                        extension =3D "";
+                        theName =3D fileName;
+                    }
+                    while (old.isValid()) {
+                        fileName =3D new StringBuffer().append(theName).ap=
pend("_").append(i).append(extension).toString();
+                        old =3D jahiaWebdavBaseService.getDAVFileAccess(pr=
ocessingContext, processingContext.getSite(),
+                                adminUser, path + "/" + fileName);
+                        i++;
+                    }
+                }
+                logger.debug("About to upload: " + fileName + " in " + pat=
h);
+                uploadDir.beginTransaction();
+                final boolean result =3D doUpload(fupload, name, paramBean=
, fileName, uploadDir);
+                if (result) {
+                    uploadDir.commitTransaction();
+                } else {
+                    uploadDir.rollbackTransaction();
+                    return null;
+                }
+                logger.debug("Finished uploading: " + result);
+                return jahiaWebdavBaseService.getDAVFileAccess(processingC=
ontext, processingContext.getSite(),
+                        adminUser, path + "/" + fileName).getDownloadURL();
+            }
+        }
+        return null;
+    }
+
     private boolean uploadFile(final DAVFileAccess dav, final ProcessingCo=
ntext processingContext) {
         final ParamBean paramBean =3D (ParamBean) processingContext;
         if (ProcessingContext.isMultipartRequest(paramBean.getRequest())) {
             final FileUpload fupload =3D paramBean.getFileUpload();
 =

-           // get files name
+            // get files name
             final Set filesName =3D fupload.getFileNames();
 =

             final Vector existingFiles =3D new Vector();
@@ -1200,11 +1317,11 @@
             if (contentType =3D=3D null) {
                 contentType =3D jParams.getContext().getMimeType(filename);
             }
-            String finalName =3D dav.uploadFile(filename, fupload.getFile(=
name), contentType, jParams.getRequest());
+            String finalName =3D dav.uploadFile(filename, f, contentType, =
jParams.getRequest());
             jParams.getRequest().getSession().setAttribute(jParams.getSite=
ID() +
                     "-justUploaded",
                     dav.getPath() + "/" + finalName);
-            return (filename !=3D null);
+            return (finalName !=3D null);
         }
         return false;
     }
@@ -1488,10 +1605,10 @@
                     // nothing to do here
                 } else {
                     if (removedCategoriesList =3D=3D null || !removedCateg=
oriesList.contains(curOldSelectedCategoryKey)) {
-                    newSelectedCategories.add(curOldSelectedCategoryKey);
+                        newSelectedCategories.add(curOldSelectedCategoryKe=
y);
+                    }
                 }
             }
-            }
             logger.debug("newSelectedCategories: " + newSelectedCategories=
);
             session.setAttribute(path + Category_Field.CATEGORIESUPDATED_E=
NGINEMAPKEY, Boolean.TRUE);
             session.setAttribute(path + Category_Field.SELECTEDCATEGORIES_=
ENGINEMAPKEY, newSelectedCategories);

_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list

Reply via email to