[ 
https://issues.apache.org/jira/browse/CAMEL-13931?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Dimitri Kotlovsky updated CAMEL-13931:
--------------------------------------
    Description: 
Hi,

I face a requirement to place temp files in a folder at the same hierarchy as 
the output folder like this:
 * {{target/data/output/claus.txt}}
 * {{target/data/temp/claus.tmp}}

Using a simple route definition like:
{code:java}
from("direct:a").to("file://target/data/output/?tempFileName=../temp/${file:name.noext}.tmp"){code}
 

If the temp folder does not exist, the GenericFileProducer should auto-create 
them. However, it only does this for the tempFileName path, if it is *relativ 
"after" the endpoint path*.

Here is a snippet from FileOperations.java that is responsible for building the 
directory:

 
{code:java}
public boolean buildDirectory(String directory, boolean absolute) throws 
GenericFileOperationFailedException {
    ObjectHelper.notNull(endpoint, "endpoint");

    // always create endpoint defined directory
    if (endpoint.isAutoCreate() && !endpoint.getFile().exists()) {
        LOG.trace("Building starting directory: {}", endpoint.getFile());
        buildDirectory(endpoint.getFile(), endpoint.getDirectoryPermissions(), 
absolute);
    }

    if (ObjectHelper.isEmpty(directory)) {
        // no directory to build so return true to indicate ok
        return true;
    }

    File endpointPath = endpoint.getFile();
    File target = new File(directory);

    File path;
    if (absolute) {
        // absolute path
        path = target;
    } else if (endpointPath.equals(target)) {
        // its just the root of the endpoint path
        path = endpointPath;
    } else {
        // relative after the endpoint path
        String afterRoot = StringHelper.after(directory, endpointPath.getPath() 
+ File.separator);
        if (ObjectHelper.isNotEmpty(afterRoot)) {
            // dir is under the root path
            path = new File(endpoint.getFile(), afterRoot);
        } else {
            // dir is relative to the root path
            path = new File(endpoint.getFile(), directory);
        }
    }

    // We need to make sure that this is thread-safe and only one thread tries 
to create the path directory at the same time.
    synchronized (this) {
        if (path.isDirectory() && path.exists()) {
            // the directory already exists
            return true;
        } else {
            LOG.trace("Building directory: {}", path);
            return buildDirectory(path, endpoint.getDirectoryPermissions(), 
absolute);
        }
    }
}{code}
 

 

A very easy way to reproduce this is to use the 'parentFileUrl' instead of the 
'fileUrl' in the createRouteBuilder method of FileProduceTempFileNameTest.java 
([https://github.com/apache/camel/blob/master/core/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java]).

  was:
Hi,

I face a requirement to place temp files in a folder at the same hierarchy as 
the output folder like this:
 * {{target/data/output/claus.txt}}
 * {{target/data/temp/claus.tmp}}

Using a simple route definition like:
{code:java}
from("direct:a").to("file://target/data/output/?tempFileName=../temp/${file:name.noext}.tmp"){code}
 

If the destination folders do not exist, the GenericFileProducer should 
auto-create them. However, it only does this for the tempFileName path, not for 
both, i.e. ../temp is generated but ../output isn't.

 

A very easy way to reproduce this is to use the 'parentFileUrl' instead of the 
'fileUrl' in the createRouteBuilder method of FileProduceTempFileNameTest.java 
(https://github.com/apache/camel/blob/master/core/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java).


> tempFileName directory is not auto-created if it is relative before the 
> endpoint path
> -------------------------------------------------------------------------------------
>
>                 Key: CAMEL-13931
>                 URL: https://issues.apache.org/jira/browse/CAMEL-13931
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 2.23.3, 2.22.5, 3.0.0.M4
>            Reporter: Dimitri Kotlovsky
>            Priority: Minor
>
> Hi,
> I face a requirement to place temp files in a folder at the same hierarchy as 
> the output folder like this:
>  * {{target/data/output/claus.txt}}
>  * {{target/data/temp/claus.tmp}}
> Using a simple route definition like:
> {code:java}
> from("direct:a").to("file://target/data/output/?tempFileName=../temp/${file:name.noext}.tmp"){code}
>  
> If the temp folder does not exist, the GenericFileProducer should auto-create 
> them. However, it only does this for the tempFileName path, if it is *relativ 
> "after" the endpoint path*.
> Here is a snippet from FileOperations.java that is responsible for building 
> the directory:
>  
> {code:java}
> public boolean buildDirectory(String directory, boolean absolute) throws 
> GenericFileOperationFailedException {
>     ObjectHelper.notNull(endpoint, "endpoint");
>     // always create endpoint defined directory
>     if (endpoint.isAutoCreate() && !endpoint.getFile().exists()) {
>         LOG.trace("Building starting directory: {}", endpoint.getFile());
>         buildDirectory(endpoint.getFile(), 
> endpoint.getDirectoryPermissions(), absolute);
>     }
>     if (ObjectHelper.isEmpty(directory)) {
>         // no directory to build so return true to indicate ok
>         return true;
>     }
>     File endpointPath = endpoint.getFile();
>     File target = new File(directory);
>     File path;
>     if (absolute) {
>         // absolute path
>         path = target;
>     } else if (endpointPath.equals(target)) {
>         // its just the root of the endpoint path
>         path = endpointPath;
>     } else {
>         // relative after the endpoint path
>         String afterRoot = StringHelper.after(directory, 
> endpointPath.getPath() + File.separator);
>         if (ObjectHelper.isNotEmpty(afterRoot)) {
>             // dir is under the root path
>             path = new File(endpoint.getFile(), afterRoot);
>         } else {
>             // dir is relative to the root path
>             path = new File(endpoint.getFile(), directory);
>         }
>     }
>     // We need to make sure that this is thread-safe and only one thread 
> tries to create the path directory at the same time.
>     synchronized (this) {
>         if (path.isDirectory() && path.exists()) {
>             // the directory already exists
>             return true;
>         } else {
>             LOG.trace("Building directory: {}", path);
>             return buildDirectory(path, endpoint.getDirectoryPermissions(), 
> absolute);
>         }
>     }
> }{code}
>  
>  
> A very easy way to reproduce this is to use the 'parentFileUrl' instead of 
> the 'fileUrl' in the createRouteBuilder method of 
> FileProduceTempFileNameTest.java 
> ([https://github.com/apache/camel/blob/master/core/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java]).



--
This message was sent by Atlassian Jira
(v8.3.2#803003)

Reply via email to