001package com.github.theholywaffle.teamspeak3.api.wrapper;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2016 Bert De Geyter, Roger Baumgartner
008 * %%
009 * Permission is hereby granted, free of charge, to any person obtaining a copy
010 * of this software and associated documentation files (the "Software"), to deal
011 * in the Software without restriction, including without limitation the rights
012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
013 * copies of the Software, and to permit persons to whom the Software is
014 * furnished to do so, subject to the following conditions:
015 * 
016 * The above copyright notice and this permission notice shall be included in
017 * all copies or substantial portions of the Software.
018 * 
019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
025 * THE SOFTWARE.
026 * #L%
027 */
028
029import java.util.Date;
030import java.util.Map;
031
032public class FileInfo extends Wrapper {
033
034        public FileInfo(Map<String, String> map) {
035                super(map);
036        }
037
038        /**
039         * Gets the ID of the channel this file is stored on.
040         *
041         * @return the ID of the file's channel
042         */
043        public int getChannelId() {
044                return getInt("cid");
045        }
046
047        /**
048         * Gets the path to this file or directory, including its name.
049         * This path can be used in other file transfer commands.
050         *
051         * @return the path to the file or directory
052         */
053        public String getPath() {
054                return get("name");
055        }
056
057        /**
058         * Gets the name of this file or directory.
059         *
060         * @return the name of this file or directory
061         */
062        public String getName() {
063                String fullPath = getPath();
064                int slashPos = fullPath.lastIndexOf('/');
065                if (slashPos < 0) return fullPath;
066                return fullPath.substring(slashPos + 1);
067        }
068
069        /**
070         * Gets the path of the directory containing this file or directory.
071         *
072         * @return the path to the parent directory
073         *
074         * @see #getPath()
075         */
076        public String getParentPath() {
077                String fullPath = getPath();
078                int slashPos = fullPath.lastIndexOf('/');
079                if (slashPos < 0) return "/";
080                return fullPath.substring(0, slashPos + 1);
081        }
082
083        /**
084         * Gets the size of the file in bytes.
085         * Note that this can return wrong values if the file is still being uploaded
086         * or if the upload has been paused by the uploading client.
087         *
088         * @return the file's size
089         */
090        public long getFileSize() {
091                return getLong("size");
092        }
093
094        /**
095         * Gets the date of the last modification to this file.
096         * The date has a one-second resolution.
097         *
098         * @return the file's last modification date
099         */
100        public Date getLastModifiedDate() {
101                return new Date(getLong("datetime") * 1000);
102        }
103
104        /**
105         * Gets whether this entry is a directory or a file.
106         * {@code 0} stands for a directory, {@code 1} for a file.
107         * <p>
108         * Consider using {@link #isFile} and {@link #isDirectory} instead.
109         * </p>
110         *
111         * @return the type of this file entry
112         */
113        public int getType() {
114                return 1;
115        }
116
117        /**
118         * Returns {@code true} if this entry is a file and not a directory.
119         *
120         * @return whether this is a file
121         */
122        public boolean isFile() {
123                return true;
124        }
125
126        /**
127         * Returns {@code true} if this entry is a directory and not a file.
128         *
129         * @return whether this is a directory
130         */
131        public boolean isDirectory() {
132                return false;
133        }
134}