Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java =================================================================== diff -u -rb776da39a329453ae50c981cc294562d907b48f5 -r0f3e85c522449f8f4741f4bae61bd07ec57fde16 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (.../ExportToolContentService.java) (revision b776da39a329453ae50c981cc294562d907b48f5) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (.../ExportToolContentService.java) (revision 0f3e85c522449f8f4741f4bae61bd07ec57fde16) @@ -35,13 +35,17 @@ import java.io.Writer; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -499,7 +503,7 @@ String secureDir = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR) + File.separator + FileUtil.LAMS_WWW_DIR + File.separator + FileUtil.LAMS_WWW_SECURE_DIR; - String ldContentDir = ExportToolContentService.getContentDirPath(ldDto.getContentFolderID()); + String ldContentDir = ExportToolContentService.getContentDirPath(ldDto.getContentFolderID(), true); ldContentDir = FileUtil.getFullPath(secureDir, ldContentDir); if (!FileUtil.isEmptyDirectory(ldContentDir, true)) { @@ -691,6 +695,16 @@ Map toolMapper = new HashMap(); Map removedActMap = new HashMap(); List activities = ldDto.getActivities(); + // LDs with version 3.0.2 have already correct paths + boolean rewriteResourcePaths = !VersionUtil.isSameOrLaterVersion("3.0.2", importedFileVersion); + // for rewriting in tool content paths like secure/4028813915760eb301157a04abe7005a/ + // into new format of paths like secure/40/28/81/39/15/76/ + String oldResourcePath = FileUtil.LAMS_WWW_SECURE_DIR + '/' + ldDto.getContentFolderID() + '/'; + String newResourcePath = FileUtil.LAMS_WWW_SECURE_DIR + '/' + + ExportToolContentService.getContentDirPath(ldDto.getContentFolderID(), false); + if (rewriteResourcePaths && ldDto.getDescription() != null) { + ldDto.setDescription(ldDto.getDescription().replaceAll(oldResourcePath, newResourcePath)); + } for (AuthoringActivityDTO activity : activities) { getLearningDesignService().fillLearningLibraryID(activity); // skip non-tool activities @@ -732,6 +746,10 @@ log.debug("Tool begin to import content : " + activity.getActivityTitle() + " by contentID :" + activity.getToolContentID()); + if (rewriteResourcePaths) { + ExportToolContentService.rewriteToolResourcePaths(toolPath, oldResourcePath, newResourcePath); + } + // tool's importToolContent() method // get from and to version String toVersion = newTool.getToolVersion(); @@ -775,7 +793,7 @@ + ldDto.getContentFolderID() + ExportToolContentService.EXPORT_LDCONTENT_ZIP_SUFFIX; String secureDir = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR) + File.separator + FileUtil.LAMS_WWW_DIR + File.separator + FileUtil.LAMS_WWW_SECURE_DIR + File.separator - + ExportToolContentService.getContentDirPath(ldDto.getContentFolderID()); + + ExportToolContentService.getContentDirPath(ldDto.getContentFolderID(), true); File contentZipFile = new File(FileUtil.getFullPath(learningDesignPath, contentZipFileName)); // unzip file to target secure dir if exists @@ -1004,6 +1022,23 @@ return toolPOJO; } + /** + * Rewrites each line of tool.xml and converts old resource paths into new format. + */ + private static void rewriteToolResourcePaths(String toolContentPath, String oldPath, String newPath) + throws IOException { + String toolFilePath = FileUtil.getFullPath(toolContentPath, ExportToolContentService.TOOL_FILE_NAME); + File toolFile = new File(toolFilePath); + Path path = toolFile.toPath(); + List oldLines = Files.readAllLines(path); + List newLines = new LinkedList(); + for (String oldLine : oldLines) { + String newLine = oldLine.replaceAll(oldPath, newPath); + newLines.add(newLine); + } + Files.write(path, newLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); + } + // ****************************************************************** // ApplicationContextAware method implementation // ****************************************************************** @@ -1961,13 +1996,14 @@ } /** - * Convert content folder ID to real path inside secure dir + * Convert content folder ID to real path inside secure dir or on server */ - private static String getContentDirPath(String contentFolderID) { + private static String getContentDirPath(String contentFolderID, boolean isFileSystemPath) { String contentFolderIDClean = contentFolderID.replaceAll("-", ""); String contentDir = ""; for (int charIndex = 0; charIndex < 6; charIndex++) { - contentDir += contentFolderIDClean.substring(charIndex * 2, charIndex * 2 + 2) + File.separator; + contentDir += contentFolderIDClean.substring(charIndex * 2, charIndex * 2 + 2) + + (isFileSystemPath ? File.separator : "/"); } return contentDir; }