Search in sources :

Example 1 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class Files method upload.

// --------------------------------------------------------------------------
private void upload(Request request) throws IOException {
    NameValuePairs name_value_pairs = new NameValuePairs();
    String filename = request.getParameter("filename");
    int parent = request.getInt("parent", 0);
    if (parent != 0)
        name_value_pairs.set("parent", parent);
    name_value_pairs.set("filename", filename);
    name_value_pairs.set("folder", false);
    name_value_pairs.set("title", request);
    int document_id = request.site.getViewDef(m_table, request.db).insert(name_value_pairs, request);
    if (document_id != -1) {
        FilePathStringBuilder dir = request.site.getBaseFilePath();
        if (m_root_dir.length() > 0)
            dir.append(m_root_dir);
        if (parent != 0)
            dir.append(parent);
        try {
            String d = dir.toString();
            new File(d).mkdirs();
            dir.append(filename);
            String file_path = dir.toString();
            request.getFileItem("filename").write(new File(file_path));
            if (filename.endsWith(".jpg") || filename.endsWith(".JPG"))
                Images.makeThumb(Images.load(file_path), d, filename, 100, true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    request.writer.write("$('c_" + getName() + "').replace()");
}
Also used : NameValuePairs(db.NameValuePairs) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File) IOException(java.io.IOException)

Example 2 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileColumn method getFile.

// --------------------------------------------------------------------------
public File getFile(int id, Request request) {
    String filename = request.db.lookupString(new Select(m_name).from(m_table).whereIdEquals(id));
    if (filename == null)
        return null;
    FilePathStringBuilder path = getDirectory(id, request);
    path.append(filename);
    return new File(path.toString());
}
Also used : Select(db.Select) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File)

Example 3 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileColumn method getFile.

// --------------------------------------------------------------------------
public File getFile(Rows data, Request request) {
    FilePathStringBuilder path = getBaseFilePath(request.site);
    if (m_dir_column != null) {
        String dir_column_value = data.getString(m_dir_column);
        if (dir_column_value != null && dir_column_value.length() > 0)
            path.append(dir_column_value);
    }
    path.append(data.getString(m_name));
    return new File(path.toString());
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File)

Example 4 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileColumn method beforeUpdate.

// --------------------------------------------------------------------------
@Override
public boolean beforeUpdate(int id, NameValuePairs name_value_pairs, Map<String, Object> previous_values, Request request) {
    if (name_value_pairs.containsName(m_name)) {
        String dir = null;
        if (m_dir_column != null)
            if (m_dir_column.equals("id"))
                dir = Integer.toString(id);
            else
                dir = name_value_pairs.getString(m_dir_column);
        String old_name = request.db.lookupString(new Select(m_name).from(m_table).whereIdEquals(id));
        if (old_name == null)
            writeTextFile(name_value_pairs, dir, true, request.site);
        else {
            writeTextFile(name_value_pairs, dir, !old_name.equals(name_value_pairs.getString(m_name)), request.site);
            if (!old_name.equals(name_value_pairs.getString(m_name)))
                deleteOldFile(name_value_pairs.getString(m_name), id, request);
        }
    }
    if (m_dir_column != null) {
        int new_dir = name_value_pairs.getInt(m_dir_column, 0);
        int old_dir = request.db.lookupInt(new Select(m_dir_column).from(m_table).whereIdEquals(id), 0);
        if (new_dir != old_dir) {
            String filename = request.db.lookupString(new Select(m_name).from(m_table).whereIdEquals(id));
            FilePathStringBuilder old_path = getBaseFilePath(request.site);
            if (old_dir != 0)
                old_path.append(old_dir);
            old_path.append(filename);
            FilePathStringBuilder new_path = getBaseFilePath(request.site);
            if (new_dir != 0)
                new_path.append(new_dir);
            new_path.append(filename);
            new File(old_path.toString()).renameTo(new File(new_path.toString()));
        }
    }
    return true;
}
Also used : Select(db.Select) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File)

Example 5 with FilePathStringBuilder

use of web.FilePathStringBuilder in project common by zenlunatics.

the class FileColumn method checkFilesInDatabase.

// --------------------------------------------------------------------------
@AdminTask({ "table", "column", "show only problems" })
public static void checkFilesInDatabase(String table, String column, boolean show_only_problems, Request request) {
    HTMLWriter writer = request.writer;
    try {
        FileColumn file_column = (FileColumn) request.site.getViewDef(table, request.db).getColumn(column);
        String dir_column = file_column.getDirColumn();
        StringBuilder sql = new StringBuilder("SELECT ").append(column);
        if (dir_column != null)
            sql.append(',').append(dir_column);
        sql.append(" FROM ").append(table).append(" ORDER BY ");
        if (dir_column != null)
            sql.append(dir_column).append(',');
        sql.append(column);
        ResultSet rs = request.db.select(sql.toString());
        writer.write("<table class=\"table table-condensed table-bordered\" style=\"width:auto;\"><tr><th>filename</th>");
        if (dir_column != null)
            writer.write("<th>dir column</th>");
        writer.write("<th>status</th></tr>");
        while (rs.next()) {
            String dir_column_value = null;
            String filename = rs.getString(1);
            String problem = null;
            if (filename == null)
                problem = column + " is null";
            else {
                if (dir_column != null)
                    dir_column_value = rs.getString(2);
                FilePathStringBuilder file_path = file_column.getDirectory(dir_column_value, request.site);
                file_path.append(filename);
                if (!new File(file_path.toString()).exists())
                    problem = "missing";
            }
            if (problem != null || !show_only_problems) {
                writer.write("<tr><td>");
                if (filename != null)
                    writer.write(filename);
                if (dir_column != null) {
                    writer.write("</td><td>");
                    if (dir_column_value != null)
                        writer.write(dir_column_value);
                }
                writer.write("</td><td>").write(problem == null ? "ok" : problem).write("</td></tr>");
            }
        }
        writer.write("</table>");
        rs.getStatement().close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : HTMLWriter(web.HTMLWriter) FilePathStringBuilder(web.FilePathStringBuilder) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) FilePathStringBuilder(web.FilePathStringBuilder) IOException(java.io.IOException) File(java.io.File) AdminTask(web.AdminTask)

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