Search in sources :

Example 6 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class PictureColumn method handleFile.

// --------------------------------------------------------------------------
@Override
public String handleFile(FileItem file_item, Mode mode, NameValuePairs name_value_pairs, Request request) {
    String error = super.handleFile(file_item, mode, name_value_pairs, request);
    if (error != null)
        return error;
    String filename = name_value_pairs.getString(m_name);
    if (filename != null && filename.length() > 0) {
        String pictures_dir = getDirectory(request).toString();
        FilePathStringBuilder original_file_path = new FilePathStringBuilder(pictures_dir);
        original_file_path.append(filename);
        BufferedImage image = null;
        if (m_max_side > 0) {
            image = Images.load(original_file_path.toString());
            if (image != null) {
                int width = image.getWidth();
                int height = image.getHeight();
                if (width > m_max_side || height > m_max_side)
                    image = Images.resizeImage(image, m_max_side);
                Images.write(image, original_file_path.toString());
            }
        }
        if (m_thumb_size > 0) {
            if (image == null)
                image = Images.load(original_file_path.toString());
            Images.makeThumb(image, pictures_dir, filename, m_thumb_size, m_size_is_max_side);
        }
        if (request.db.getTable(m_table).getColumn("width") != null) {
            if (image == null)
                image = Images.load(original_file_path.toString());
            if (image != null) {
                name_value_pairs.set("width", image.getWidth());
                name_value_pairs.set("height", image.getHeight());
            }
        }
    }
    return null;
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) BufferedImage(java.awt.image.BufferedImage)

Example 7 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class Pictures method syncPictures.

// --------------------------------------------------------------------------
@AdminTask
public void syncPictures(Request request) {
    FilePathStringBuilder file_path = request.site.getBaseFilePath();
    file_path.append(m_table);
    String pictures_directory = file_path.toString() + File.separator;
    String[] files = new File(pictures_directory).list();
    for (String file : files) {
        String id = file;
        int index = id.indexOf('.');
        if (index != -1)
            id = id.substring(0, index);
        if (Character.isDigit(file.charAt(0)) && request.db.lookupString("id", "pictures", Integer.parseInt(id)) == null)
            new File(pictures_directory + file).delete();
    }
    files = new File(pictures_directory + "thumbs/").list();
    for (String file : files) if (Character.isDigit(file.charAt(0))) {
        String id = file;
        int index = id.indexOf('.');
        if (index != -1)
            id = id.substring(0, index);
        if (request.db.lookupString("id", "pictures", Integer.parseInt(id)) == null)
            new File(pictures_directory + "thumbs/" + file).delete();
    }
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File) AdminTask(web.AdminTask)

Example 8 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class Pictures method doPost.

// --------------------------------------------------------------------------
@Override
public boolean doPost(Request request) throws IOException {
    if (super.doPost(request))
        return true;
    int id = request.getPathSegmentInt(1);
    String action = request.getPathSegment(2);
    FilePathStringBuilder file_path = request.site.getBaseFilePath();
    file_path.append(m_table);
    String filename = request.db.lookupString("file", m_table, id);
    String new_filename = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf('.'));
    if (action.endsWith(" clockwise"))
        Images.rotateClockwise(file_path.toString(), filename, new_filename, m_thumb_size);
    else
        Images.rotateCounterclockwise(file_path.toString(), filename, new_filename, m_thumb_size);
    request.db.update(m_table, "file='" + new_filename + "'", id);
    String path = file_path.append(new_filename).toString();
    request.writer.write(path.substring(path.indexOf("pictures")));
    return true;
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder)

Example 9 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileManager method writeDirectory.

// --------------------------------------------------------------------------
private void writeDirectory(String path, Request request) throws IOException {
    HTMLWriter writer = request.writer;
    writer.write("<ul class=\"breadcrumb\" style=\"background-color:#eee\">");
    if (path.length() > 0) {
        String[] dirs = path.split("/");
        String p = "";
        writer.write("<li><a href=\"#\" onclick=\"change_dir('')\">/</a></li>");
        for (int i = 0; i < dirs.length - 1; i++) {
            if (i > 0)
                p += "/";
            p += dirs[i];
            writer.write("<li><a href=\"#\" onclick=\"change_dir('").write(p).write("')\">").write(dirs[i]).write("</a></li>");
        }
        writer.write("<li>").write(dirs[dirs.length - 1]).write("</li>");
    } else
        writer.write("<li>/</li>");
    writer.write("</ul>");
    Table table = new Table(request.writer);
    table.tr();
    writer.setAttribute("valign", "top");
    table.td();
    FilePathStringBuilder baseFilePath = getRoot(request);
    if (path.length() > 0)
        baseFilePath.append(path);
    File[] files = new File(baseFilePath.toString()).listFiles();
    Arrays.sort(files, new Comparator<File>() {

        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareToIgnoreCase(o2.getName());
        }
    });
    Table table2 = new Table(writer);
    Date date = new Date();
    DateFormat df = DateFormat.getDateTimeInstance();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    for (File file : files) {
        String name = file.getName();
        if (name.charAt(0) != '.' && !name.equals("CVS") && !name.equals("WEB-INF")) {
            table2.tr().td();
            if (file.isDirectory()) {
                writer.setAttribute("style", "color:black;");
                writer.aOnClickOpen("change_dir('" + (path.length() > 0 ? path + "/" + name : name) + "')");
                writer.setAttribute("style", "padding-right:3px;margin-bottom:-2px;");
                writer.img("folder.png");
                writer.write(name);
                writer.tagClose();
            } else if (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".gif") || name.endsWith(".pdf")) {
                writer.setAttribute("style", "color:black;");
                String src = request.getContext();
                if (path.length() > 0)
                    src += "/" + path.replaceAll("\\\\", "/");
                writer.aOnClickOpen("$('file').set('html','<img src=&quot;" + src + "/" + name + "&quot; />')");
                writer.setAttribute("style", "padding-right:3px;margin-bottom:-2px;");
                writer.img("page_white.png");
                writer.write(name);
                writer.tagClose();
            // } else if (name.endsWith(".jsp") || name.endsWith(".htm") || name.endsWith(".html") || name.endsWith(".js") || name.endsWith(".css")) {
            // writer.setAttribute("style", "color:black;");
            // writer.aOnClickOpen("$('file').replace(context+'/FileManager'+(fm_dir==''?'':'/'+fm_dir)+'?action=edit&file=" + name + "')");
            // writer.setAttribute("style", "padding-right:3px;margin-bottom:-2px;");
            // writer.img("page_white.png");
            // writer.write(name);
            // writer.tagClose();
            } else {
                writer.setAttribute("style", "padding-right:3px;margin-bottom:-2px;");
                writer.img("page_white.png");
                writer.write(name);
            }
            date.setTime(file.lastModified());
            long length = file.length();
            writer.setAttribute("style", "padding:0 20px;text-align:right;");
            table2.td(length < 1024 ? Long.toString(length) : length < 1048576 ? nf.format((double) length / 1024) + " k" : length < 1073741824 ? nf.format((double) length / 1048576) + " M" : nf.format((double) length / 1073741824) + " G");
            table2.td(df.format(date));
        }
    }
    table2.close();
    writer.setAttribute("valign", "top");
    writer.setAttribute("id", "file");
    table.td();
    table.close();
}
Also used : HTMLWriter(web.HTMLWriter) Table(web.Table) Date(java.util.Date) DateFormat(java.text.DateFormat) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File) NumberFormat(java.text.NumberFormat)

Example 10 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileColumn method handleFile.

// --------------------------------------------------------------------------
public String handleFile(FileItem file_item, Mode mode, NameValuePairs name_value_pairs, Request request) {
    String filename = file_item.getName();
    if (filename.length() == 0)
        return null;
    filename = filename.replace('\\', '-').replace('/', '-');
    filename = filename.replace("%20", " ");
    FilePathStringBuilder file_path = getDirectory(request);
    File dir = new File(file_path.toString());
    if (!dir.exists())
        if (!dir.mkdirs())
            request.abort("problem with directory " + dir.getAbsolutePath());
    file_path.append(filename);
    File file = new File(file_path.toString());
    if (mode == View.Mode.ADD_FORM)
        if (m_generate_file_names) {
            String extension = filename.substring(filename.lastIndexOf('.'));
            try {
                file = File.createTempFile(extension.substring(1), extension, dir);
                filename = file.getName();
            } catch (IOException e) {
                request.abort(e);
            }
        } else if (file.exists())
            return "A file with the name " + filename + " has already been uploaded";
    try {
        file_item.write(file);
        if (mode == View.Mode.EDIT_FORM)
            deleteOldFile(filename, request.getInt("db_key_value", 0), request);
    } catch (Exception e) {
        request.abort(e);
    }
    name_value_pairs.set(m_name, filename);
    return null;
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) IOException(java.io.IOException) File(java.io.File) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FilePathStringBuilder (web.FilePathStringBuilder)13 File (java.io.File)11 IOException (java.io.IOException)5 AdminTask (web.AdminTask)3 Select (db.Select)2 SQLException (java.sql.SQLException)2 HTMLWriter (web.HTMLWriter)2 ICalendar (biweekly.ICalendar)1 VEvent (biweekly.component.VEvent)1 TimezoneAssignment (biweekly.io.TimezoneAssignment)1 NameValuePairs (db.NameValuePairs)1 BufferedImage (java.awt.image.BufferedImage)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 ResultSet (java.sql.ResultSet)1 DateFormat (java.text.DateFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 TimeZone (java.util.TimeZone)1