Last active
October 10, 2022 09:49
-
-
Save CorneAussems/a2598cceb9a9570cd96c01177fe6d635 to your computer and use it in GitHub Desktop.
Remove old File versions from DL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Date; | |
import java.time.*; | |
import java.text.SimpleDateFormat; | |
import java.nio.file.*; | |
import com.liferay.portal.kernel.model.*; | |
import com.liferay.portal.kernel.service.*; | |
import com.liferay.portal.kernel.repository.model.*; | |
import com.liferay.portal.kernel.dao.orm.QueryUtil; | |
import com.liferay.document.library.kernel.model.*; | |
import com.liferay.document.library.kernel.service.*; | |
import com.liferay.document.library.kernel.store.DLStoreUtil; | |
import org.apache.commons.io.*; | |
now = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); | |
outputFile = new File("""${System.getProperty("liferay.home")}/logs/remove_old_versions_from_dl_${now}.log""") | |
outputFile.getParentFile().mkdirs() | |
cleanupDate = Date.from(LocalDate.now().minusMonths(1).atStartOfDay(ZoneId.systemDefault()).toInstant()); | |
countFiles=0; | |
countFileSize=0; | |
getCompanies(); | |
log("countFiles:"+countFiles); | |
log("countFileSize:"+FileUtils.byteCountToDisplaySize(countFileSize)); | |
def getCompanies() { | |
allCompanies = CompanyLocalServiceUtil.getCompanies(); | |
for (Company company:allCompanies) { | |
log("Company:"+company.companyId); | |
getGroups(company.companyId); | |
} | |
} | |
def getGroups(companyId) { | |
allGroups = GroupLocalServiceUtil.getCompanyGroups(companyId,QueryUtil.ALL_POS, QueryUtil.ALL_POS); | |
for (Group group:allGroups) { | |
if(group.isRegularSite()){ | |
log(companyId+" : "+group.groupId+" : "+group.friendlyURL +" : "+group.regularSite +" : "+group.organization ) | |
listFiles(group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID); | |
} | |
} | |
} | |
def listFiles(groupId,folderId) { | |
try{ | |
allfiles = DLAppServiceUtil.getFileEntries(groupId,folderId) | |
for (FileEntry file:allfiles) { | |
//log("File Title: " + file.getTitle() +" : "+ file.getVersion()); | |
List results = file.getFileVersions(-1); | |
latestversion = file.getVersion(); | |
for(FileVersion fv : results) { | |
if(fv.getVersion() != latestversion && fv.getModifiedDate().before(cleanupDate) ){ | |
try{ | |
log("Deleting "+ file.title + " "+ latestversion + " >> " + fv.version ); | |
realfile = DLFileEntryLocalServiceUtil.getFile(file.getFileEntryId(),file.getFileVersion().getVersion(),false); | |
Path path = Paths.get(realfile.getCanonicalPath()); | |
countFileSize+=Files.size(path); | |
countFiles++; | |
// DLAppServiceUtil.deleteFileVersion(file.getFileEntryId(), fv.getVersion()); | |
} catch (e){ | |
log("listFiles() getFile exception : "+e) | |
} | |
} | |
} | |
} | |
allfolders = DLAppServiceUtil.getFolders(groupId,folderId); | |
for (Folder folder:allfolders) { | |
// log("Folder Name: " + folder.getName()); | |
listFiles(groupId,folder.getFolderId()); | |
} | |
} catch (e){ | |
log("listFiles() exception : "+e) | |
} | |
} | |
def log(message) { | |
out.println(message) | |
outputFile << "${message}\n" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment