Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileTagList {
- private List<FileTagType> tags = new ArrayList<FileTagType>();
- private BitSet visibility = new BitSet();
- public boolean isVisible (int tag) {
- return tag < this.tags.size() && this.visibility.get(tag);
- }
- public void setVisible (int tag, boolean visible) {
- if (tag < this.tags.size()) this.visibility.set(tag, visible);
- }
- public FileTagType getTag (int tag) {
- return this.tags.get(tag);
- }
- public void setTag (int tag, FileTagType ftag, boolean visible) {
- while (this.tags.size() <= tag) this.tags.add(null);
- this.tags.set(tag, ftag);
- this.visibility.set(tag, visible);
- }
- }
- public interface FileTagType {
- String getName ();
- String getValue (File f);
- void setValue (File f, String s);
- }
- public class FileNameTagType {
- public String getName () {
- return "File Name";
- }
- public String getValue (File f) {
- return f.getName();
- }
- public void setValue (File f, String s) {
- try {
- Files.move(f.toPath(), new File(f.getParent(), s).toPath(), StandardCopyOption.ATOMIC_MOVE);
- } catch (IOException ioe) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file!", ioe);
- } catch (FileAlreadyExistsException faee) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file because its target already exists!", faee);
- } catch (AtomicMoveNotSupportedException | UnsupportedOperationException ee) {
- try {
- Files.move(f.toPath(), new File(f.getParent(), s).toPath());
- } catch (IOException | UnsupportedOperationException e) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file!", e);
- } catch (FileAlreadyExistsException faee) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file because its target already exists!", faee);
- } catch (SecurityException se) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file because of security issues!", se);
- }
- } catch (SecurityException se) {
- TagSmith.getLog().log(Level.WARNING, "Could not move file because of security issues!", se);
- }
- }
- }
- public class FileExtensionTagType {
- private static final HashMap<String, String> RESERVED_NAMES = new HashMap<String, String>();
- static {
- RESERVED_NAMES.put("TXT", "Text file");
- RESERVED_NAMES.put("ZIP", "Zip archive file");
- RESERVED_NAMES.put("RAR", "WinRAR archive file");
- RESERVED_NAMES.put("7Z", "7Zip archive file");
- RESERVED_NAMES.put("BMP", "Bitmap image file");
- RESERVED_NAMES.put("PNG", "PNG image file");
- RESERVED_NAMES.put("JPG", "JPEG image file");
- RESERVED_NAMES.put("JPEG", "JPEG image file");
- RESERVED_NAMES.put("WAV", "Microsoft WAVE audio file");
- RESERVED_NAMES.put("MP3", "MPEG audio file");
- RESERVED_NAMES.put("MID", "MIDI sequence file");
- RESERVED_NAMES.put("MIDI", "MIDI sequence file");
- RESERVED_NAMES.put("MP4", "MPEG video file");
- RESERVED_NAMES.put("EXE", "Windows executable file");
- RESERVED_NAMES.put("DLL", "Windows library file");
- RESERVED_NAMES.put("ELF", "Linux executable file");
- }
- public String getName () {
- return "File Extension";
- }
- public String getValue (File f) {
- String s = f.getName();
- if (s.lastIndexOf(".") == -1) return "File";
- String ext = s.substring(s.lastIndexOf(".") + 1).toUpperCase();
- if (RESERVED_NAMES.containsKey()) return RESERVED_NAMES.get(ext);
- return ext + " file";
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "To change a file's extension, try renaming it instead.");
- }
- }
- public class FileSizeTagType {
- public static final String[] LENGTH_NAMES = new String[] {
- " Bytes",
- " Bytes",
- " KiB",
- " KB",
- " MiB",
- " MB",
- " GiB",
- " GB",
- " TiB",
- " TB",
- " PiB",
- " PB",
- " EiB",
- " EB"
- };
- public String getName () {
- return "File Size";
- }
- public String getValue (File f) {
- double size = Files.readAttributes(f.toPath(), BasicFileAttributes.class).size();
- int threshold = Integer.parseInt(TagSmith.getProperty("threshold"));
- int sizemod = 0;
- while (size > (threshold * 4)) {
- sizemod++;
- size /= threshold;
- }
- String s = size;
- s = s.substring(0, Math.min(5, s.length()));
- if (s.endsWith(".")) s = s.substring(0, s.length() - 1);
- return s + LENGTH_NAMES[(sizemod << 1) | ((threshold & 8) >> 3)];
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "Cannot set file sizes!");
- }
- }
- public class FileTypeTagType {
- public String getName () {
- return "File Type";
- }
- public String getValue (File f) {
- BasicFileAttributes attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
- if (attrs.isRegularFile()) return "File";
- else if (attrs.isDirectory()) return "Folder";
- else if (attrs.isSymbolicLink()) return "Shortcut";
- else return "Other";
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "Cannot set file types!");
- }
- }
- public class FileCreationTimeTagType {
- public String getName () {
- return "File System Creation Time";
- }
- public String getValue (File f) {
- FileTime time = Files.readAttributes(f.toPath(), BasicFileAttributes.class).creationTime();
- return DateTimeFormatter.RFC_1123_DATE_TIME.format(time.toInstant());
- }
- public void setValue (File f, String s) {
- TemporalAccessor acc;
- try {
- acc = DateTimeFormatter.RFC_1123_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- try {
- acc = DateTimeFormatter.ISO_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- TagSmith.getLog().log(Level.WARNING, "Could not parse date!", ioe);
- return;
- }
- }
- Files.setAttribute(f.toPath(), "basic:creationTime", FileTime.from(Instant.from(acc)));
- }
- }
- public class FileModificationTimeTagType {
- public String getName () {
- return "File System Last Modified Time";
- }
- public String getValue (File f) {
- FileTime time = Files.readAttributes(f.toPath(), BasicFileAttributes.class).lastModifiedTime();
- return DateTimeFormatter.RFC_1123_DATE_TIME.format(time.toInstant());
- }
- public void setValue (File f, String s) {
- TemporalAccessor acc;
- try {
- acc = DateTimeFormatter.RFC_1123_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- try {
- acc = DateTimeFormatter.ISO_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- TagSmith.getLog().log(Level.WARNING, "Could not parse date!", ioe);
- return;
- }
- }
- Files.setAttribute(f.toPath(), "basic:lastModifiedTime", FileTime.from(Instant.from(acc)));
- }
- }
- public class FileAccessTimeTagType {
- public String getName () {
- return "File System Last Access Time";
- }
- public String getValue (File f) {
- FileTime time = Files.readAttributes(f.toPath(), BasicFileAttributes.class).lastAccessTime();
- return DateTimeFormatter.RFC_1123_DATE_TIME.format(time.toInstant());
- }
- public void setValue (File f, String s) {
- TemporalAccessor acc;
- try {
- acc = DateTimeFormatter.RFC_1123_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- try {
- acc = DateTimeFormatter.ISO_DATE_TIME.parse(s);
- } catch (DateTimeParseException dtpe) {
- TagSmith.getLog().log(Level.WARNING, "Could not parse date!", ioe);
- return;
- }
- }
- Files.setAttribute(f.toPath(), "basic:lastAccessTime", FileTime.from(Instant.from(acc)));
- }
- }
- public class FileOwnerTagType {
- public String getName () {
- return "File Owner";
- }
- public String getValue (File f) {
- return Files.getOwner(f.toPath()).getName();
- }
- public void setValue (File f, String s) {
- UserPrincipalLookupService upls = f.toPath().getFileSystem().getUserPrincipalLookupService();
- UserPrincipal up;
- try {
- up = upls.lookupPrincipalByName(s);
- } catch (UserPrincipalNotFoundException upnfee) {
- try {
- up = upls.lookupPrincipalByGroupName(s);
- } catch (UserPrincipalNotFoundException upnfe) {
- TagSmith.getLog().log(Level.WARNING, "Could not find user!", upnfe);
- }
- }
- Files.getFileAttributeView(f.toPath(), FileOwnerAttributeView.class).setOwner(up);
- }
- }
- public class QTMediaMap {
- // javafx.scene.media.Media
- public static HashMap<File, Media> mediamap = new HashMap<File, Media>();
- }
- public class FileQTDurationTagType {
- public String getName () {
- return "File QuickTime Duration";
- }
- public String getValue (File f) {
- return QTMediaMap.mediamap.computeIfAbsent(f, f -> new Media(f.toURI().toString())).getDuration().toString();
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "Cannot set QuickTime durations!");
- }
- }
- public class FileQTDimensionsTagType {
- public String getName () {
- return "File QuickTime Dimensions";
- }
- public String getValue (File f) {
- return QTMediaMap.mediamap.computeIfAbsent(f, f -> new Media(f.toURI().toString())).getDuration().toString();
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "Cannot set QuickTime dimensions!");
- }
- }
- public class FileQTDimensionsTagType {
- public String getName () {
- return "File QuickTime Dimensions";
- }
- public String getValue (File f) {
- Media m = QTMediaMap.mediamap.computeIfAbsent(f, f -> new Media(f.toURI().toString()));
- return m.getWidth() + " by " + m.getHeight() + " pixels";
- }
- public void setValue (File f, String s) {
- TagSmith.getLog().log(Level.WARNING, "Cannot set QuickTime dimensions!");
- }
- }
Advertisement