Check if a file exists

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Check if a file exists

ajwagner777
So I need some suggestions.  I am making some enhancements to our departmental templates and I would like to dynamically pull in an image if it exists, from a certain folder.

For example, say I have two departments on the same host, one is 'dept-one' and the other is 'dept-two', and both use the same template on all pages.   In each departments folder, there exists a 'gfx' folder which is the home for the department specific images.  I want to look in that gfx folder (again, it will be relative to the folder it's in) and display an image that is specific to that folder, if it exists.  So if the first dept, 'dept-one' has 'dept-header-image.jpg' in the 'gfx' folder and the second dept, 'dept-two' does not have and image by that same name in its 'gfx' folder, then 'dept-one' will have the image loaded onto the page, and the second will not.

I have looked at the webapi and utilmethods API's and have not found anything that might help.  I COULD look into the file repository macro, but there are cases that a dept might have 100's of images in that 'gfx' folder, and I don't want the overhead of sorting through ALL those files to pull out a single one.

I hope this makes sense.   Thanks in advance!

Aaron Wagner
University of Texas Health Science Center at Houston
Web Developer II
University Web Communications
7000 Fannin, Suite 1540-2
University Center Tower
Phone: 713-500-3459
Fax: 713-500-3052
Email: [hidden email]<mailto:[hidden email]>
Web: www.uthouston.edu<http://www.uthouston.edu/>
CONFIDENTIALITY: This e-mail (including any attachments) may contain confidential, proprietary and privileged information, and unauthorized disclosure or use is prohibited. If you received this e-mail in error, please notify the sender and delete this e-mail from your system.

Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

Jim Serio-2
Aaron,

To my knowledge there is no "exists" or similar method to locate a file.
The only solution I have is using the file repository and looping through
comparing each file.

Another option may be to create a structure for key images.  Then, in the
template, query the structure for an image based on the URI (or some other
predefine criteria).  If it exists, display the image defined in the
structure.  If not, display some default/catch-all image.

Jim


On Tue, Mar 9, 2010 at 5:21 PM, Wagner, Aaron <[hidden email]>wrote:

>
>
>  So I need some suggestions.  I am making some enhancements to our
> departmental templates and I would like to dynamically pull in an image if
> it exists, from a certain folder.
>
>
>
> For example, say I have two departments on the same host, one is ‘dept-one’
> and the other is ‘dept-two’, and both use the same template on all pages.
>  In each departments folder, there exists a ‘gfx’ folder which is the home
> for the department specific images.  I want to look in that gfx folder
> (again, it will be relative to the folder it’s in) and display an image that
> is specific to that folder, if it exists.  So if the first dept, ‘dept-one’
> has ‘dept-header-image.jpg’ in the ‘gfx’ folder and the second dept,
> ‘dept-two’ does not have and image by that same name in its ‘gfx’ folder,
> then ‘dept-one’ will have the image loaded onto the page, and the second
> will not.
>
>
>
> I have looked at the webapi and utilmethods API’s and have not found
> anything that might help.  I COULD look into the file repository macro, but
> there are cases that a dept might have 100’s of images in that ‘gfx’ folder,
> and I don’t want the overhead of sorting through ALL those files to pull out
> a single one.
>
>
>
> I hope this makes sense.   Thanks in advance!
>
>
>
> *Aaron Wagner*
> *University of Texas Health Science Center at Houston**
> *Web Developer II
>
> University Web Communications
>
> 7000 Fannin, Suite 1540-2
> University Center Tower
> Phone: 713-500-3459
> Fax: 713-500-3052
> Email: [hidden email]
> Web: www.uthouston.edu
>
> CONFIDENTIALITY: This e-mail (including any attachments) may contain
> confidential, proprietary and privileged information, and unauthorized
> disclosure or use is prohibited. If you received this e-mail in error,
> please notify the sender and delete this e-mail from your system.
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

Maria Ahues Bouza
I would recommend doing what Jim says by adding a field with the image to
the Department structure. That would allow you to check if the image exists
in the template based on the content you're displaying.


On Tue, Mar 9, 2010 at 7:52 PM, Jim Serio <[hidden email]> wrote:

>
>
> Aaron,
>
> To my knowledge there is no "exists" or similar method to locate a file.
> The only solution I have is using the file repository and looping through
> comparing each file.
>
> Another option may be to create a structure for key images.  Then, in the
> template, query the structure for an image based on the URI (or some other
> predefine criteria).  If it exists, display the image defined in the
> structure.  If not, display some default/catch-all image.
>
> Jim
>
>
>
> On Tue, Mar 9, 2010 at 5:21 PM, Wagner, Aaron <[hidden email]>wrote:
>
>>
>>
>>  So I need some suggestions.  I am making some enhancements to our
>> departmental templates and I would like to dynamically pull in an image if
>> it exists, from a certain folder.
>>
>>
>>
>> For example, say I have two departments on the same host, one is
>> ‘dept-one’ and the other is ‘dept-two’, and both use the same template on
>> all pages.   In each departments folder, there exists a ‘gfx’ folder which
>> is the home for the department specific images.  I want to look in that gfx
>> folder (again, it will be relative to the folder it’s in) and display an
>> image that is specific to that folder, if it exists.  So if the first dept,
>> ‘dept-one’ has ‘dept-header-image.jpg’ in the ‘gfx’ folder and the second
>> dept, ‘dept-two’ does not have and image by that same name in its ‘gfx’
>> folder, then ‘dept-one’ will have the image loaded onto the page, and the
>> second will not.
>>
>>
>>
>> I have looked at the webapi and utilmethods API’s and have not found
>> anything that might help.  I COULD look into the file repository macro, but
>> there are cases that a dept might have 100’s of images in that ‘gfx’ folder,
>> and I don’t want the overhead of sorting through ALL those files to pull out
>> a single one.
>>
>>
>>
>> I hope this makes sense.   Thanks in advance!
>>
>>
>>
>> *Aaron Wagner*
>> *University of Texas Health Science Center at Houston**
>> *Web Developer II
>>
>> University Web Communications
>>
>> 7000 Fannin, Suite 1540-2
>> University Center Tower
>> Phone: 713-500-3459
>> Fax: 713-500-3052
>> Email: [hidden email]
>> Web: www.uthouston.edu
>>
>> CONFIDENTIALITY: This e-mail (including any attachments) may contain
>> confidential, proprietary and privileged information, and unauthorized
>> disclosure or use is prohibited. If you received this e-mail in error,
>> please notify the sender and delete this e-mail from your system.
>>
>>
>>
>>
>>
>  
>



--
Maria Ahues Bouza
Managing Director, Professional Services

dotCMS
Main: 305.858.1422
Direct: 786.594.5272
Fax: 305.397.2579
www.dotcms.org
http://www.twitter.com/dotCMS
http://www.twitter.com/mabouza

"Packt Publishing 2009 Finalist for Best Other Open Source CMS"

Please consider the planet before printing this email.
Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

Jim Serio-2
After re-reading this, I see that you will have one template that will be
used for different sites and it appears that based on the department, you
want a different header image displayed.  One additional suggestion is to
add a header container to the template.  Then, when the user creates the
page, they add the appropriate image to the header container.  Since you can
reuse the containers on each page, it only needs to be done once and the
container can be locked for regular users (so they don't go modifying
them).  I think this makes more sense since it's guaranteed you will end up
with more departments using this template which will require more code
modifications to your template (to check even more directories).  I can see
this snowballing pretty fast :-)

Jim

On Tue, Mar 9, 2010 at 5:56 PM, Maria Ahues Bouza <
[hidden email]> wrote:

>
>
> I would recommend doing what Jim says by adding a field with the image to
> the Department structure. That would allow you to check if the image exists
> in the template based on the content you're displaying.
>
>
> On Tue, Mar 9, 2010 at 7:52 PM, Jim Serio <[hidden email]> wrote:
>
>>
>>
>> Aaron,
>>
>> To my knowledge there is no "exists" or similar method to locate a file.
>> The only solution I have is using the file repository and looping through
>> comparing each file.
>>
>> Another option may be to create a structure for key images.  Then, in the
>> template, query the structure for an image based on the URI (or some other
>> predefine criteria).  If it exists, display the image defined in the
>> structure.  If not, display some default/catch-all image.
>>
>> Jim
>>
>>
>>
>> On Tue, Mar 9, 2010 at 5:21 PM, Wagner, Aaron <[hidden email]>wrote:
>>
>>>
>>>
>>>  So I need some suggestions.  I am making some enhancements to our
>>> departmental templates and I would like to dynamically pull in an image if
>>> it exists, from a certain folder.
>>>
>>>
>>>
>>> For example, say I have two departments on the same host, one is
>>> ‘dept-one’ and the other is ‘dept-two’, and both use the same template on
>>> all pages.   In each departments folder, there exists a ‘gfx’ folder which
>>> is the home for the department specific images.  I want to look in that gfx
>>> folder (again, it will be relative to the folder it’s in) and display an
>>> image that is specific to that folder, if it exists.  So if the first dept,
>>> ‘dept-one’ has ‘dept-header-image.jpg’ in the ‘gfx’ folder and the second
>>> dept, ‘dept-two’ does not have and image by that same name in its ‘gfx’
>>> folder, then ‘dept-one’ will have the image loaded onto the page, and the
>>> second will not.
>>>
>>>
>>>
>>> I have looked at the webapi and utilmethods API’s and have not found
>>> anything that might help.  I COULD look into the file repository macro, but
>>> there are cases that a dept might have 100’s of images in that ‘gfx’ folder,
>>> and I don’t want the overhead of sorting through ALL those files to pull out
>>> a single one.
>>>
>>>
>>>
>>> I hope this makes sense.   Thanks in advance!
>>>
>>>
>>>
>>> *Aaron Wagner*
>>> *University of Texas Health Science Center at Houston**
>>> *Web Developer II
>>>
>>> University Web Communications
>>>
>>> 7000 Fannin, Suite 1540-2
>>> University Center Tower
>>> Phone: 713-500-3459
>>> Fax: 713-500-3052
>>> Email: [hidden email]
>>> Web: www.uthouston.edu
>>>
>>> CONFIDENTIALITY: This e-mail (including any attachments) may contain
>>> confidential, proprietary and privileged information, and unauthorized
>>> disclosure or use is prohibited. If you received this e-mail in error,
>>> please notify the sender and delete this e-mail from your system.
>>>
>>>
>>>
>>>
>>>
>>
>
>
> --
> Maria Ahues Bouza
> Managing Director, Professional Services
>
> dotCMS
> Main: 305.858.1422
> Direct: 786.594.5272
> Fax: 305.397.2579
> www.dotcms.org
> http://www.twitter.com/dotCMS
> http://www.twitter.com/mabouza
>
> "Packt Publishing 2009 Finalist for Best Other Open Source CMS"
>
> Please consider the planet before printing this email.
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

wezell-2
Administrator
In reply to this post by Maria Ahues Bouza
I think there is a hacky way to get at it - the WebAPI viewtool has a method
called getAssetPath() that you can pass a path.  If the file exists, it
should return a "real asset path".  If not, it will return an empty string.
 It could work for you with a little elbow grease.

Will

----
Will Ezell
www.dotcms.org
phone: 305.858.1422 ext. 286
direct: 978.296.4239
skype: wezell1




On Tue, Mar 9, 2010 at 7:56 PM, Maria Ahues Bouza <
[hidden email]> wrote:

>
>
> I would recommend doing what Jim says by adding a field with the image to
> the Department structure. That would allow you to check if the image exists
> in the template based on the content you're displaying.
>
>
> On Tue, Mar 9, 2010 at 7:52 PM, Jim Serio <[hidden email]> wrote:
>
>>
>>
>> Aaron,
>>
>> To my knowledge there is no "exists" or similar method to locate a file.
>> The only solution I have is using the file repository and looping through
>> comparing each file.
>>
>> Another option may be to create a structure for key images.  Then, in the
>> template, query the structure for an image based on the URI (or some other
>> predefine criteria).  If it exists, display the image defined in the
>> structure.  If not, display some default/catch-all image.
>>
>> Jim
>>
>>
>>
>> On Tue, Mar 9, 2010 at 5:21 PM, Wagner, Aaron <[hidden email]>wrote:
>>
>>>
>>>
>>>  So I need some suggestions.  I am making some enhancements to our
>>> departmental templates and I would like to dynamically pull in an image if
>>> it exists, from a certain folder.
>>>
>>>
>>>
>>> For example, say I have two departments on the same host, one is
>>> ‘dept-one’ and the other is ‘dept-two’, and both use the same template on
>>> all pages.   In each departments folder, there exists a ‘gfx’ folder which
>>> is the home for the department specific images.  I want to look in that gfx
>>> folder (again, it will be relative to the folder it’s in) and display an
>>> image that is specific to that folder, if it exists.  So if the first dept,
>>> ‘dept-one’ has ‘dept-header-image.jpg’ in the ‘gfx’ folder and the second
>>> dept, ‘dept-two’ does not have and image by that same name in its ‘gfx’
>>> folder, then ‘dept-one’ will have the image loaded onto the page, and the
>>> second will not.
>>>
>>>
>>>
>>> I have looked at the webapi and utilmethods API’s and have not found
>>> anything that might help.  I COULD look into the file repository macro, but
>>> there are cases that a dept might have 100’s of images in that ‘gfx’ folder,
>>> and I don’t want the overhead of sorting through ALL those files to pull out
>>> a single one.
>>>
>>>
>>>
>>> I hope this makes sense.   Thanks in advance!
>>>
>>>
>>>
>>> *Aaron Wagner*
>>> *University of Texas Health Science Center at Houston**
>>> *Web Developer II
>>>
>>> University Web Communications
>>>
>>> 7000 Fannin, Suite 1540-2
>>> University Center Tower
>>> Phone: 713-500-3459
>>> Fax: 713-500-3052
>>> Email: [hidden email]
>>> Web: www.uthouston.edu
>>>
>>> CONFIDENTIALITY: This e-mail (including any attachments) may contain
>>> confidential, proprietary and privileged information, and unauthorized
>>> disclosure or use is prohibited. If you received this e-mail in error,
>>> please notify the sender and delete this e-mail from your system.
>>>
>>>
>>>
>>>
>>>
>>
>
>
> --
> Maria Ahues Bouza
> Managing Director, Professional Services
>
> dotCMS
> Main: 305.858.1422
> Direct: 786.594.5272
> Fax: 305.397.2579
> www.dotcms.org
> http://www.twitter.com/dotCMS
> http://www.twitter.com/mabouza
>
> "Packt Publishing 2009 Finalist for Best Other Open Source CMS"
>
> Please consider the planet before printing this email.
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

Ramon-24
In reply to this post by ajwagner777
How about creating a viewtool plugin to call from a vtl page. You can probably instead load the file path from plugin properties. Something like below.

public class FileViewTool implements ViewTool {
public boolean fileExists(File file) {
if (File.exists())
  return true;
else
  return false;
}
}
in Velocity page
#set($file = "/html/images/x.gif")
#if ($yourviewtool.fileExists($file))
....
#else
....
#end

Thanks,
Ramon

--- In [hidden email], "Wagner, Aaron" <aaron.wagner@...> wrote:

>
> So I need some suggestions.  I am making some enhancements to our departmental templates and I would like to dynamically pull in an image if it exists, from a certain folder.
>
> For example, say I have two departments on the same host, one is 'dept-one' and the other is 'dept-two', and both use the same template on all pages.   In each departments folder, there exists a 'gfx' folder which is the home for the department specific images.  I want to look in that gfx folder (again, it will be relative to the folder it's in) and display an image that is specific to that folder, if it exists.  So if the first dept, 'dept-one' has 'dept-header-image.jpg' in the 'gfx' folder and the second dept, 'dept-two' does not have and image by that same name in its 'gfx' folder, then 'dept-one' will have the image loaded onto the page, and the second will not.
>
> I have looked at the webapi and utilmethods API's and have not found anything that might help.  I COULD look into the file repository macro, but there are cases that a dept might have 100's of images in that 'gfx' folder, and I don't want the overhead of sorting through ALL those files to pull out a single one.
>
> I hope this makes sense.   Thanks in advance!
>
> Aaron Wagner
> University of Texas Health Science Center at Houston
> Web Developer II
> University Web Communications
> 7000 Fannin, Suite 1540-2
> University Center Tower
> Phone: 713-500-3459
> Fax: 713-500-3052
> Email: aaron.wagner@...<mailto:aaron.wagner@...>
> Web: www.uthouston.edu<http://www.uthouston.edu/>
> CONFIDENTIALITY: This e-mail (including any attachments) may contain confidential, proprietary and privileged information, and unauthorized disclosure or use is prohibited. If you received this e-mail in error, please notify the sender and delete this e-mail from your system.
>


Reply | Threaded
Open this post in threaded view
|

RE: Check if a file exists

ajwagner777
In reply to this post by wezell-2
Will, this worked perfectly.  I thought I would share the code I came up with in case anyone wanted to refer to it for their own purposes.

#set($folderName = $macro.getFolderPath($webapi.getSubURIByDepth(1), $request))
#set($imgPath = $folderName + 'gfx/Special.gif')
#set($pathToTest = $webapi.getAssetPath($imgPath))
#if($pathToTest != '')
  <img src="$imgPath">
#end

I didn't like the file repository solution because it would put a lot of overhead on our already stretched server, particularly if there were hundreds of images for it to sort through.  Thanks again Will, you're a life saver!

Aaron Wagner
University of Texas Health Science Center at Houston
Web Developer II
University Web Communications
7000 Fannin, Suite 1540-2
University Center Tower
Phone: 713-500-3459
Fax: 713-500-3052
Email: [hidden email]<mailto:[hidden email]>
Web: www.uthouston.edu<http://www.uthouston.edu/>
CONFIDENTIALITY: This e-mail (including any attachments) may contain confidential, proprietary and privileged information, and unauthorized disclosure or use is prohibited. If you received this e-mail in error, please notify the sender and delete this e-mail from your system.

From: [hidden email] [mailto:[hidden email]] On Behalf Of Will Ezell
Sent: Tuesday, March 09, 2010 7:06 PM
To: [hidden email]
Subject: Re: [dotcms] Check if a file exists



I think there is a hacky way to get at it - the WebAPI viewtool has a method called getAssetPath() that you can pass a path.  If the file exists, it should return a "real asset path".  If not, it will return an empty string.  It could work for you with a little elbow grease.

Will

----
Will Ezell
www.dotcms.org<http://www.dotcms.org>
phone: 305.858.1422 ext. 286
direct: 978.296.4239
skype: wezell1



On Tue, Mar 9, 2010 at 7:56 PM, Maria Ahues Bouza <[hidden email]<mailto:[hidden email]>> wrote:


I would recommend doing what Jim says by adding a field with the image to the Department structure. That would allow you to check if the image exists in the template based on the content you're displaying.

On Tue, Mar 9, 2010 at 7:52 PM, Jim Serio <[hidden email]<mailto:[hidden email]>> wrote:


Aaron,

To my knowledge there is no "exists" or similar method to locate a file.  The only solution I have is using the file repository and looping through comparing each file.

Another option may be to create a structure for key images.  Then, in the template, query the structure for an image based on the URI (or some other predefine criteria).  If it exists, display the image defined in the structure.  If not, display some default/catch-all image.

Jim


On Tue, Mar 9, 2010 at 5:21 PM, Wagner, Aaron <[hidden email]<mailto:[hidden email]>> wrote:

So I need some suggestions.  I am making some enhancements to our departmental templates and I would like to dynamically pull in an image if it exists, from a certain folder.

For example, say I have two departments on the same host, one is 'dept-one' and the other is 'dept-two', and both use the same template on all pages.   In each departments folder, there exists a 'gfx' folder which is the home for the department specific images.  I want to look in that gfx folder (again, it will be relative to the folder it's in) and display an image that is specific to that folder, if it exists.  So if the first dept, 'dept-one' has 'dept-header-image.jpg' in the 'gfx' folder and the second dept, 'dept-two' does not have and image by that same name in its 'gfx' folder, then 'dept-one' will have the image loaded onto the page, and the second will not.

I have looked at the webapi and utilmethods API's and have not found anything that might help.  I COULD look into the file repository macro, but there are cases that a dept might have 100's of images in that 'gfx' folder, and I don't want the overhead of sorting through ALL those files to pull out a single one.

I hope this makes sense.   Thanks in advance!

Aaron Wagner
University of Texas Health Science Center at Houston
Web Developer II
University Web Communications
7000 Fannin, Suite 1540-2
University Center Tower
Phone: 713-500-3459
Fax: 713-500-3052
Email: [hidden email]<mailto:[hidden email]>
Web: www.uthouston.edu<http://www.uthouston.edu/>
CONFIDENTIALITY: This e-mail (including any attachments) may contain confidential, proprietary and privileged information, and unauthorized disclosure or use is prohibited. If you received this e-mail in error, please notify the sender and delete this e-mail from your system.





--
Maria Ahues Bouza
Managing Director, Professional Services

dotCMS
Main: 305.858.1422
Direct: 786.594.5272
Fax: 305.397.2579
www.dotcms.org<http://www.dotcms.org>
http://www.twitter.com/dotCMS
http://www.twitter.com/mabouza

"Packt Publishing 2009 Finalist for Best Other Open Source CMS"

Please consider the planet before printing this email.



Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

Hafsa
In reply to this post by Ramon-24
Hi Ramon,
Will you tell me how to add viewtool plugin in detail? I am new to dotCMS and java and facing a lot of problems in it. I have successfull added a new host to dotCMS and deploy it but now I am stuck on plugins. Will you guide me to write a new plugin, deploy it and use it in dotCMS templates.

Thanks. Waiting for your response.
Hafsa
Reply | Threaded
Open this post in threaded view
|

Re: Check if a file exists

futbolor14
In reply to this post by ajwagner777
I'm using the following code to check for the existence of a folder,
which works well:
#set($pathToTest = $webapi.getAssetPath('/mobile'))#if($pathToTest !=
'')    #* do stuff *##end