001    package de.deepamehta.plugins.files;
002    
003    import de.deepamehta.core.JSONEnabled;
004    import de.deepamehta.core.util.JavaUtils;
005    
006    import org.codehaus.jettison.json.JSONArray;
007    import org.codehaus.jettison.json.JSONObject;
008    
009    import java.io.File;
010    import java.util.ArrayList;
011    import java.util.List;
012    
013    
014    
015    public class DirectoryListing implements JSONEnabled {
016    
017        // ------------------------------------------------------------------------------------------------------- Constants
018    
019        private static final String FILE_REPOSITORY_PATH = System.getProperty("dm4.filerepo.path");
020    
021        // ---------------------------------------------------------------------------------------------- Instance Variables
022    
023        private FileItem dirInfo;
024        private List<FileItem> fileItems = new ArrayList<FileItem>();
025    
026        // ---------------------------------------------------------------------------------------------------- Constructors
027    
028        public DirectoryListing(File directory) {
029            dirInfo = new FileItem(directory.getName(), directory.getPath());
030            for (File file : directory.listFiles()) {
031                String name = file.getName();
032                String path = file.getPath();
033                if (file.isDirectory()) {
034                    fileItems.add(new FileItem(name, path));
035                } else {
036                    fileItems.add(new FileItem(name, path, file.length(), JavaUtils.getFileType(file.getName())));
037                }
038            }
039        }
040    
041        // -------------------------------------------------------------------------------------------------- Public Methods
042    
043        public List<FileItem> getFileItems() {
044            return fileItems;
045        }
046    
047        // ---
048    
049        @Override
050        public JSONObject toJSON() {
051            try {
052                JSONObject dir = dirInfo.toJSON();
053                JSONArray items = new JSONArray();
054                for (FileItem item : fileItems) {
055                    items.put(item.toJSON());
056                }
057                dir.put("items", items);
058                return dir;
059            } catch (Exception e) {
060                throw new RuntimeException("Serialization failed (" + this + ")", e);
061            }
062        }
063    
064        // --------------------------------------------------------------------------------------------------- Inner Classes
065    
066        public class FileItem implements JSONEnabled {
067    
068            ItemKind kind;  // FILE or DIRECTORY
069            String name;
070            String path;
071            long size;      // for files only
072            String type;    // for files only
073    
074            /**
075             * Constructs a DIRECTORY item.
076             */
077            FileItem(String name, String path) {
078                this.kind = ItemKind.DIRECTORY;
079                this.name = name;
080                this.path = truncate(path);
081            }
082    
083            /**
084             * Constructs a FILE item.
085             */
086            FileItem(String name, String path, long size, String type) {
087                this.kind = ItemKind.FILE;
088                this.name = name;
089                this.path = truncate(path);
090                this.size = size;
091                this.type = type;
092            }
093    
094            // ---
095    
096            public ItemKind getItemKind() {
097                return kind;
098            }
099    
100            public String getName() {
101                return name;
102            }
103    
104            public String getPath() {
105                return path;
106            }
107    
108            public long getSize() {
109                return size;
110            }
111    
112            public String getMediaType() {
113                return type;
114            }
115    
116            // ---
117    
118            private String truncate(String path) {
119                // error check
120                if (!path.startsWith(FILE_REPOSITORY_PATH)) {
121                    throw new RuntimeException("Path \"" + path + "\" is not a file repository path");
122                }
123                //
124                return JavaUtils.stripDriveLetter(path.substring(FILE_REPOSITORY_PATH.length()));
125            }
126    
127            // ---
128    
129            @Override
130            public JSONObject toJSON() {
131                try {
132                    JSONObject item = new JSONObject();
133                    item.put("kind", kind.stringify());
134                    item.put("name", name);
135                    item.put("path", path);
136                    if (kind.equals("file")) {
137                        item.put("size", size);
138                        item.put("type", type);
139                    }
140                    return item;
141                } catch (Exception e) {
142                    throw new RuntimeException("Serialization failed (" + this + ")", e);
143                }
144            }
145        }
146    }