diff --git a/src/main/java/org/olat/modules/glossary/GlossaryManagerImpl.java b/src/main/java/org/olat/modules/glossary/GlossaryManagerImpl.java index bda093c15809726aee997b6c5babae3827931820..c4ec7fb5b3d3f673695ae6b359e1e72507b2fa13 100644 --- a/src/main/java/org/olat/modules/glossary/GlossaryManagerImpl.java +++ b/src/main/java/org/olat/modules/glossary/GlossaryManagerImpl.java @@ -20,29 +20,22 @@ package org.olat.modules.glossary; import java.io.File; -import java.io.IOException; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.lucene.document.Document; import org.olat.core.commons.modules.glossary.GlossaryItemManager; -import org.olat.core.gui.media.CleanupAfterDeliveryFileMediaResource; import org.olat.core.gui.media.MediaResource; +import org.olat.core.gui.media.ZippedContainerMediaResource; import org.olat.core.id.OLATResourceable; -import org.apache.logging.log4j.Logger; -import org.olat.core.logging.Tracing; import org.olat.core.util.StringHelper; -import org.olat.core.util.WebappHelper; -import org.olat.core.util.ZipUtil; import org.olat.core.util.filter.Filter; import org.olat.core.util.filter.FilterFactory; -import org.olat.core.util.vfs.LocalFileImpl; import org.olat.core.util.vfs.LocalFolderImpl; import org.olat.core.util.vfs.VFSContainer; import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSLeaf; -import org.olat.core.util.vfs.filters.VFSSystemItemFilter; import org.olat.course.CourseFactory; import org.olat.course.ICourse; import org.olat.course.config.CourseConfig; @@ -73,8 +66,6 @@ import org.springframework.stereotype.Service; @Service("glossaryManager") public class GlossaryManagerImpl implements GlossaryManager { - private static final Logger log = Tracing.createLoggerFor(GlossaryManagerImpl.class); - private static final String EXPORT_FOLDER_NAME = "glossary"; @Autowired @@ -179,19 +170,9 @@ public class GlossaryManagerImpl implements GlossaryManager { public MediaResource getAsMediaResource(OLATResourceable res) { RepositoryEntry repoEntry = RepositoryManager.getInstance().lookupRepositoryEntry(res, false); String exportFileName = repoEntry.getDisplayname(); - // OO-135 check for special / illegal chars in filename exportFileName = StringHelper.transformDisplayNameToFileSystemName(exportFileName); - - try { - File tmpDir = new File(WebappHelper.getTmpDir()); - File fExportZIP = File.createTempFile(exportFileName, ".zip", tmpDir); - VFSContainer glossaryRoot = getGlossaryRootFolder(res); - ZipUtil.zip(glossaryRoot.getItems(new VFSSystemItemFilter()), new LocalFileImpl(fExportZIP), false); - return new CleanupAfterDeliveryFileMediaResource(fExportZIP); - } catch (IOException e) { - log.error("Cannot export glossar: " + res, e); - return null; - } + VFSContainer glossaryRoot = getGlossaryRootFolder(res); + return new ZippedContainerMediaResource(exportFileName, glossaryRoot); } diff --git a/src/main/java/org/olat/modules/webFeed/manager/FeedManagerImpl.java b/src/main/java/org/olat/modules/webFeed/manager/FeedManagerImpl.java index 5e6558329b5f96c30de60547a9a97116d7794110..9d7c77d72822d37566e926b8fbf47309bb58500d 100644 --- a/src/main/java/org/olat/modules/webFeed/manager/FeedManagerImpl.java +++ b/src/main/java/org/olat/modules/webFeed/manager/FeedManagerImpl.java @@ -40,6 +40,7 @@ import org.olat.core.commons.services.image.Size; import org.olat.core.commons.services.notifications.NotificationsManager; import org.olat.core.commons.services.vfs.VFSRepositoryService; import org.olat.core.gui.components.form.flexible.elements.FileElement; +import org.olat.core.gui.media.CleanupAfterDeliveryFileMediaResource; import org.olat.core.gui.media.MediaResource; import org.olat.core.helpers.Settings; import org.olat.core.id.Identity; @@ -53,8 +54,8 @@ import org.olat.core.util.ZipUtil; import org.olat.core.util.coordinate.Coordinator; import org.olat.core.util.coordinate.CoordinatorManager; import org.olat.core.util.coordinate.LockResult; -import org.olat.core.util.coordinate.SyncerCallback; import org.olat.core.util.resource.OresHelper; +import org.olat.core.util.vfs.LocalFileImpl; import org.olat.core.util.vfs.LocalFolderImpl; import org.olat.core.util.vfs.Quota; import org.olat.core.util.vfs.QuotaManager; @@ -786,8 +787,11 @@ public class FeedManagerImpl extends FeedManager { } @Override - public VFSMediaResource getFeedArchiveMediaResource(OLATResourceable resource) { + public MediaResource getFeedArchiveMediaResource(OLATResourceable resource) { VFSLeaf zip = getFeedArchive(resource); + if(zip instanceof LocalFileImpl) { + return new CleanupAfterDeliveryFileMediaResource(((LocalFileImpl) zip).getBasefile()); + } return new VFSMediaResource(zip); } @@ -821,18 +825,15 @@ public class FeedManagerImpl extends FeedManager { // synchronize all zip processes for this feed // o_clusterOK by:fg - VFSLeaf zip = coordinator.getSyncer().doInSync(resource, new SyncerCallback<VFSLeaf>() { - @Override - public VFSLeaf execute() { - // Delete the old archive and recreate it from scratch - String zipFileName = getZipFileName(resource); - VFSItem oldArchive = rootContainer.resolve(zipFileName); - if (oldArchive != null) { - oldArchive.delete(); - } - ZipUtil.zip(feedContainer.getItems(), rootContainer.createChildLeaf(zipFileName), false); - return (VFSLeaf) rootContainer.resolve(zipFileName); + VFSLeaf zip = coordinator.getSyncer().doInSync(resource, () -> { + // Delete the old archive and recreate it from scratch + String zipFileName = getFeedKind(resource) + ".zip"; + VFSItem oldArchive = rootContainer.resolve(zipFileName); + if (oldArchive != null) { + oldArchive.delete(); } + ZipUtil.zip(feedContainer.getItems(), rootContainer.createChildLeaf(zipFileName), false); + return (VFSLeaf) rootContainer.resolve(zipFileName); }); // delete the XML files again. They are only needed for the export. @@ -844,17 +845,6 @@ public class FeedManagerImpl extends FeedManager { return zip; } - /** - * Returns the file name of the archive that is to be exported. Depends on - * the kind of the resource. - * - * @param resource - * @return The zip archive file name - */ - private String getZipFileName(OLATResourceable resource) { - return getFeedKind(resource) + ".zip"; - } - @Override public Feed loadFeedFromXML(Path feedDir) { return feedFileStorage.loadFeedFromXML(feedDir); diff --git a/src/main/java/org/olat/repository/handlers/CourseHandler.java b/src/main/java/org/olat/repository/handlers/CourseHandler.java index d6b107b0a26ce77aa88440b7075a46ebcaa46e36..daff764afa9624a23d536768c1d7a1a09ca25154 100644 --- a/src/main/java/org/olat/repository/handlers/CourseHandler.java +++ b/src/main/java/org/olat/repository/handlers/CourseHandler.java @@ -342,7 +342,7 @@ public class CourseHandler implements RepositoryHandler { SharedFolderFileResource resource = sfm.createSharedFolder(); if (resource == null) { - log.error("Error adding file resource during repository reference import: " + importExport.getDisplayName()); + log.error("Error adding file resource during repository reference import: {}", importExport.getDisplayName()); } // unzip contents diff --git a/src/main/java/org/olat/upgrade/OLATUpgrade_14_2_6.java b/src/main/java/org/olat/upgrade/OLATUpgrade_14_2_6.java new file mode 100644 index 0000000000000000000000000000000000000000..926737f6033c9d0b487196aa49706313f98598f6 --- /dev/null +++ b/src/main/java/org/olat/upgrade/OLATUpgrade_14_2_6.java @@ -0,0 +1,163 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.upgrade; + +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.Logger; +import org.olat.core.commons.modules.bc.FolderConfig; +import org.olat.core.commons.persistence.DB; +import org.olat.core.logging.Tracing; +import org.olat.core.util.FileUtils; +import org.olat.core.util.StringHelper; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * + * Initial date: 2 avr. 2020<br> + * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com + * + */ +public class OLATUpgrade_14_2_6 extends OLATUpgrade { + + private static final Logger log = Tracing.createLoggerFor(OLATUpgrade_14_2_6.class); + + private static final String VERSION = "OLAT_14.2.6"; + private static final String DELETE_COURSE_EXPORT_DIRECTORY = "DELETE COURSE EXPORT DIRECTORY"; + private static final String DELETE_DOWNLOAD_ZIP = "DELETE DOWNLOAD ZIP"; + + @Autowired + private DB dbInstance; + + public OLATUpgrade_14_2_6() { + super(); + } + + @Override + public String getVersion() { + return VERSION; + } + + @Override + public boolean doPostSystemInitUpgrade(UpgradeManager upgradeManager) { + UpgradeHistoryData uhd = upgradeManager.getUpgradesHistory(VERSION); + if (uhd == null) { + // has never been called, initialize + uhd = new UpgradeHistoryData(); + } else if (uhd.isInstallationComplete()) { + return false; + } + + boolean allOk = true; + allOk &= deleteExportDirectory(upgradeManager, uhd); + allOk &= deleteDownloadZip(upgradeManager, uhd); + + uhd.setInstallationComplete(allOk); + upgradeManager.setUpgradesHistory(uhd, VERSION); + if(allOk) { + log.info(Tracing.M_AUDIT, "Finished OLATUpgrade_14_2_6 successfully!"); + } else { + log.info(Tracing.M_AUDIT, "OLATUpgrade_14_2_6 not finished, try to restart OpenOlat!"); + } + return allOk; + } + + private boolean deleteExportDirectory(UpgradeManager upgradeManager, UpgradeHistoryData uhd) { + boolean allOk = true; + if (!uhd.getBooleanDataValue(DELETE_COURSE_EXPORT_DIRECTORY)) { + Path courseDirectory = Paths.get(FolderConfig.getCanonicalRoot(), "course"); + + List<Long> courses = new ArrayList<>(); + try(DirectoryStream<Path> directoryStream = Files.newDirectoryStream(courseDirectory)) { + for (Path path : directoryStream) { + String name = path.getFileName().toString(); + if(StringHelper.containsNonWhitespace(name)) { + Path exportDirectory = path.resolve("export"); + if(Files.exists(exportDirectory)) { + courses.add(Long.parseLong(name)); + } + } + } + } catch (Exception e) { + log.error("", e); + allOk = false; + } + + for(Long course:courses) { + deleteCourseExportDirectory(course); + } + + uhd.setBooleanDataValue(DELETE_COURSE_EXPORT_DIRECTORY, allOk); + upgradeManager.setUpgradesHistory(uhd, VERSION); + } + return allOk; + } + + private void deleteCourseExportDirectory(Long courseId) { + try { + Path courseExportDirectory = Paths.get(FolderConfig.getCanonicalRoot(), "course", courseId.toString(), "export"); + if(Files.exists(courseExportDirectory)) { + FileUtils.deleteDirsAndFiles(courseExportDirectory.toFile(), true, true); + dbInstance.commitAndCloseSession(); + log.info("Delete export directory of course with id: {}", courseId); + } + } catch (Exception e) { + log.error("", e); + } + } + + private boolean deleteDownloadZip(UpgradeManager upgradeManager, UpgradeHistoryData uhd) { + boolean allOk = true; + if (!uhd.getBooleanDataValue(DELETE_DOWNLOAD_ZIP)) { + + String[] downloadNames = new String[] { "imscp.zip", "blog.zip", "podcast.zip" }; + + Path repositoryDirectory = Paths.get(FolderConfig.getCanonicalRepositoryHome()); + try(DirectoryStream<Path> directoryStream = Files.newDirectoryStream(repositoryDirectory)) { + for (Path path : directoryStream) { + String name = path.getFileName().toString(); + if(StringHelper.containsNonWhitespace(name)) { + for(String downloadName:downloadNames) { + Path backupZip = path.resolve(downloadName); + if(Files.exists(backupZip)) { + FileUtils.deleteFile(backupZip.toFile()); + log.info("Delete download zip {} for resource with id: {}", downloadName, name); + dbInstance.commitAndCloseSession(); + } + } + } + } + } catch (Exception e) { + log.error("", e); + allOk = false; + } + + uhd.setBooleanDataValue(DELETE_DOWNLOAD_ZIP, allOk); + upgradeManager.setUpgradesHistory(uhd, VERSION); + } + return allOk; + } + +} diff --git a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml index 36b8cb62665f5ea5815a93f80086bc400b63b2fd..e7f846dafd6c5113dde494eabaa7a0270d61a256 100644 --- a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml +++ b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml @@ -54,6 +54,7 @@ <bean id="upgrade_14_0_5" class="org.olat.upgrade.OLATUpgrade_14_0_5"/> <bean id="upgrade_14_1_0" class="org.olat.upgrade.OLATUpgrade_14_1_0"/> <bean id="upgrade_14_2_0" class="org.olat.upgrade.OLATUpgrade_14_2_0"/> + <bean id="upgrade_14_2_6" class="org.olat.upgrade.OLATUpgrade_14_2_6"/> </list> </property> </bean>