Commit 45cf8717 authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

Main refactoring of put file logic

parent d3bbc525
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public class GetFileController extends FileController {
                    throw PermissionDeniedException.forPath(path);
                }

                File file = new File(fileInfo.getOsPath());
                File file = new File(fileInfo.getFsPath());
                FileResponseUtil.getFileResponse(response, file, path);
            } else {
                throw new NodeNotFoundException(path);
+53 −13
Original line number Diff line number Diff line
@@ -47,7 +47,8 @@ public class FileDAO {
                + "accept_views, provide_views, l.location_type, n.path <> n.relative_path AS virtual_parent,\n"
                + "(SELECT user_name FROM users WHERE user_id = creator_id) AS username, n.job_id,\n"
                + "base_path, get_os_path(n.node_id) AS os_path, ? AS vos_path, false AS is_directory,\n"
                + "type = 'link' AS is_link\n"
                + "type = 'link' AS is_link,\n"
                + "fs_path \n"
                + "FROM node n\n"
                + "JOIN location l ON (n.location_id IS NOT NULL AND n.location_id = l.location_id) OR (n.location_id IS NULL AND l.location_id = ?)\n"
                + "LEFT JOIN storage s ON s.storage_id = l.storage_dest_id\n"
@@ -111,22 +112,25 @@ public class FileDAO {
    }

    public void updateFileAttributes(int nodeId, 
            String fsPath,
            String contentType,
            String contentEncoding,
            Long contentLength,
            String contentMd5) {

        String sql = "UPDATE node SET content_type = ?, content_encoding = ?, content_length = ?, content_md5 = ?, location_id = ? "
        String sql = "UPDATE node SET fs_path = ?, content_type = ?, content_encoding = ?, content_length = ?, content_md5 = ?, location_id = ? "
                + "WHERE node_id = ?";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, contentType);
            ps.setString(2, contentEncoding);
            ps.setLong(3, contentLength);
            ps.setString(4, contentMd5);
            ps.setInt(5, uploadLocationId);
            ps.setInt(6, nodeId);
            int i = 0;
            ps.setString(++i, fsPath);
            ps.setString(++i, contentType);
            ps.setString(++i, contentEncoding);
            ps.setLong(++i, contentLength);
            ps.setString(++i, contentMd5);
            ps.setInt(++i, uploadLocationId);
            ps.setInt(++i, nodeId);
            return ps;
        });

@@ -170,7 +174,7 @@ public class FileDAO {
            throw new IllegalArgumentException("Received empty list of paths");
        }

        String sql = "SELECT n.node_id, n.is_public, n.group_read, n.group_write, n.creator_id, n.async_trans,\n"
        String sql = "SELECT n.node_id, n.is_public, n.group_read, n.group_write, n.creator_id, n.async_trans, n.fs_path,\n"
                + "n.content_type, n.content_encoding, n.content_length, n.content_md5,\n"
                + "n.accept_views, n.provide_views, l.location_type, n.path <> n.relative_path AS virtual_parent,\n"
                + "(SELECT user_name FROM users WHERE user_id = n.creator_id) AS username,\n"
@@ -203,7 +207,7 @@ public class FileDAO {
    // TODO: same problem as get archive file infos
    public List<FileInfo> getBranchFileInfos(String rootVosPath, String jobId) {

        String sql = "SELECT n.node_id, n.is_public, n.group_read, n.group_write, n.creator_id, n.async_trans,\n"
        String sql = "SELECT n.node_id, n.is_public, n.group_read, n.group_write, n.creator_id, n.async_trans, n.fs_path\n"
                + "n.content_type, n.content_encoding, n.content_length, n.content_md5,\n"
                + "n.accept_views, n.provide_views, l.location_type, n.path <> n.relative_path AS virtual_parent,\n"
                + "(SELECT user_name FROM users WHERE user_id = n.creator_id) AS username,\n"
@@ -297,11 +301,47 @@ public class FileDAO {
            fi.setLocationType(rs.getString("location_type"));
        }

        fillOsPath(fi, rs);
        this.fillActualBasePath(fi, rs);
        this.fillFsPath(fi, rs);

        return fi;
    }

    private void fillActualBasePath(FileInfo fi, ResultSet rs) throws SQLException {
        String basePath = rs.getString("base_path");
        if (basePath == null) {
            return;
        }

        Path completeBasePath = Path.of(basePath);

        boolean asyncLocation = "async".equals(rs.getString("location_type"));

        if (asyncLocation) {
            String username = rs.getString("username");
            completeBasePath = completeBasePath.resolve(username).resolve("retrieve");
        }

        fi.setActualBasePath(completeBasePath.toString());
    }

    private void fillFsPath(FileInfo fi, ResultSet rs) throws SQLException {

        String fsPath = rs.getString("fs_path");

        if (fsPath == null) {
            return;
        }

        if (fsPath.startsWith("/")) {
            fsPath = fsPath.substring(1);
        }

        Path completeFsPath = Path.of(fsPath);

        fi.setFsPath(completeFsPath.toString());
    }

    private void fillOsPath(FileInfo fi, ResultSet rs) throws SQLException {

        String basePath = rs.getString("base_path");
+30 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 */
package it.inaf.ia2.transfer.persistence.model;

import java.nio.file.Path;
import java.util.List;

public class FileInfo {
@@ -13,6 +14,10 @@ public class FileInfo {
    private String osPath;
    private String virtualPath;
    private String virtualName;
    private String fsPath;
    // actualBasePath differs from base path in db due to some location type
    // dependent manipulations (performed by FileDAO)
    private String actualBasePath;
    private boolean isPublic;
    private boolean virtualParent;
    private boolean directory;
@@ -199,6 +204,31 @@ public class FileInfo {
        this.jobId = jobId;
    }

    public String getFsPath() {
        return fsPath;
    }

    public void setFsPath(String fsPath) {
        this.fsPath = fsPath;
    }

    public String getActualBasePath() {
        return actualBasePath;
    }

    public void setActualBasePath(String actualBasePath) {
        this.actualBasePath = actualBasePath;
    }

    // This function returns the full path of this file on storage
    public String getFilePath() {
        if (this.actualBasePath == null || this.fsPath == null) {
            return null;
        } else {
            return Path.of(this.actualBasePath).resolve(this.fsPath).toString();
        }
    }

    public static String getVosParentPath(FileInfo fileInfo) {
        return fileInfo.getVirtualPath().substring(0, fileInfo.getVirtualPath().lastIndexOf("/"));
    }
+1 −1
Original line number Diff line number Diff line
@@ -308,7 +308,7 @@ public class ArchiveService {
            throw PermissionDeniedException.forPath(fileInfo.getVirtualPath());
        }

        File file = new File(fileInfo.getOsPath());
        File file = new File(fileInfo.getFsPath());
        LOG.trace("Adding file " + file.getAbsolutePath() + " to tar archive");

        try ( InputStream is = new FileInputStream(file)) {
+2 −2
Original line number Diff line number Diff line
@@ -206,8 +206,8 @@ public class FileCopyService {
            throw PermissionDeniedException.forPath(sourceFileInfo.getVirtualPath());
        }

        File file = new File(sourceFileInfo.getOsPath());
        LOG.trace("Copying file: {} to {}",file.getAbsolutePath(), destinationFileInfo.getOsPath());
        File file = new File(sourceFileInfo.getFsPath());
        LOG.trace("Copying file: {} to {}",file.getAbsolutePath(), destinationFileInfo.getFsPath());

        putFileService.copyLocalFile(sourceFileInfo, destinationFileInfo, remainingQuota);

Loading