-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move global configuration to Appearance category (#278)
- Loading branch information
1 parent
2625833
commit 924b26a
Showing
23 changed files
with
646 additions
and
380 deletions.
There are no files selected for viewing
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
126 changes: 126 additions & 0 deletions
126
src/main/java/jenkins/plugins/foldericon/CustomFolderIconConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2023 strangelookingnerd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package jenkins.plugins.foldericon; | ||
|
||
import com.cloudbees.hudson.plugins.folder.AbstractFolder; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.FilePath; | ||
import hudson.model.PageDecorator; | ||
import jenkins.appearance.AppearanceCategory; | ||
import jenkins.model.GlobalConfigurationCategory; | ||
import jenkins.model.Jenkins; | ||
import org.apache.commons.io.FileUtils; | ||
import org.kohsuke.stapler.HttpResponse; | ||
import org.kohsuke.stapler.HttpResponses; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.interceptor.RequirePOST; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
/** | ||
* The global Custom Folder Icon configuration. | ||
* | ||
* @author strangelookingnerd | ||
*/ | ||
@Extension | ||
public class CustomFolderIconConfiguration extends PageDecorator { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CustomFolderIconConfiguration.class.getName()); | ||
|
||
public static final String PLUGIN_PATH = "customFolderIcons"; | ||
|
||
public static final String USER_CONTENT_PATH = "userContent"; | ||
|
||
@NonNull | ||
@Override | ||
public GlobalConfigurationCategory getCategory() { | ||
return GlobalConfigurationCategory.get(AppearanceCategory.class); | ||
} | ||
|
||
/** | ||
* Get human-readable disk-usage of all icons. | ||
* | ||
* @return OK | ||
*/ | ||
@SuppressWarnings("unused") | ||
@NonNull | ||
public String getDiskUsage() { | ||
Jenkins.get().checkPermission(Jenkins.ADMINISTER); | ||
|
||
FilePath iconDir = Jenkins.get().getRootPath().child(USER_CONTENT_PATH).child(PLUGIN_PATH); | ||
|
||
Set<String> existingIcons = CustomFolderIcon.getAvailableIcons(); | ||
|
||
long total = 0L; | ||
|
||
for (String icon : existingIcons) { | ||
try { | ||
total += iconDir.child(icon).length(); | ||
} catch (IOException | InterruptedException ex) { | ||
LOGGER.log(Level.WARNING, ex, () -> "Unable to determine size for Folder Icon '" + icon + "'!"); | ||
} | ||
} | ||
return FileUtils.byteCountToDisplaySize(total); | ||
} | ||
|
||
/** | ||
* Cleanup unused icons. | ||
* | ||
* @param req the request | ||
* @return OK | ||
*/ | ||
@RequirePOST | ||
public HttpResponse doCleanup(@SuppressWarnings("unused") StaplerRequest req) { | ||
Jenkins.get().checkPermission(Jenkins.ADMINISTER); | ||
|
||
FilePath iconDir = Jenkins.get().getRootPath().child(USER_CONTENT_PATH).child(PLUGIN_PATH); | ||
|
||
Set<String> existingIcons = CustomFolderIcon.getAvailableIcons(); | ||
|
||
Set<String> usedIcons = Jenkins.get().getAllItems(AbstractFolder.class).stream() | ||
.filter(folder -> folder.getIcon() instanceof CustomFolderIcon) | ||
.map(folder -> ((CustomFolderIcon) folder.getIcon()).getFoldericon()) | ||
.collect(Collectors.toSet()); | ||
|
||
if (usedIcons.isEmpty() || existingIcons.removeAll(usedIcons)) { | ||
for (String icon : existingIcons) { | ||
try { | ||
if (!iconDir.child(icon).delete()) { | ||
LOGGER.warning(() -> "Unable to delete unused Folder Icon '" + icon + "'!"); | ||
} | ||
} catch (IOException | InterruptedException ex) { | ||
LOGGER.log(Level.WARNING, ex, () -> "Unable to delete unused Folder Icon '" + icon + "'!"); | ||
} | ||
} | ||
} | ||
return HttpResponses.ok(); | ||
} | ||
} |
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
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
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
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
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
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
File renamed without changes.
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
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
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
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
Oops, something went wrong.