%%% BEGIN openflax/handler/random.erl %%% %%% %%% openflax - Open Source web server for Erlang/OTP %%% Copyright (c)2003 Cat's Eye Technologies. All rights reserved. %%% %%% Redistribution and use in source and binary forms, with or without %%% modification, are permitted provided that the following conditions %%% are met: %%% %%% Redistributions of source code must retain the above copyright %%% notice, this list of conditions and the following disclaimer. %%% %%% Redistributions in binary form must reproduce the above copyright %%% notice, this list of conditions and the following disclaimer in %%% the documentation and/or other materials provided with the %%% distribution. %%% %%% Neither the name of Cat's Eye Technologies nor the names of its %%% contributors may be used to endorse or promote products derived %%% from this software without specific prior written permission. %%% %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND %%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, %%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF %%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE %%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE %%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, %%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, %%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON %%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, %%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY %%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE %%% POSSIBILITY OF SUCH DAMAGE. %% @doc Display a random file from a certain directory. %% %%

This handler's security/featurefulness tradeoff level is %% 3 (Unobtrusive.) If not all the files are of the same %% content type, it might confuse some user agents. %% The res_content_type setting should be specified %% in the config file.

%% %%

This handler requires openflax.handler.file.

%% %% @end -module(openflax.handler.random). -vsn('$Id: random.erl 31 2004-04-23 07:00:11Z catseye $'). -author('cpressey@catseye.mine.nu'). -copyright('Copyright (c)2003 Cat`s Eye Technologies. All rights reserved.'). -behaviour(openflax.handler). -export([start/1, stop/1, serve/1]). % behaviour -import(lists). -import(random). -import(filename). -import(file). %% @spec start(conf()) -> conf() %% @doc Initializes the random-file-serving subsystem. start(Conf) -> {A1, A2, A3} = now(), random:seed(A3, A2, A1), openflax.conf:new(). %% @spec stop(conf()) -> conf() %% @doc Shuts down the random-file serving subsystem. stop(Conf) -> Conf. %% @spec serve(conf()) -> {response(), conf()} %% @doc Serves a random file from this directory. serve(Conf) -> Dir = filename:dirname(openflax.handler.file:document(Conf)), {ok, Files} = file:list_dir(Dir), Filename = pick(Dir, Files, Conf), % set new basic resource BasicResource = openflax.conf:get_string(sreq_basic_resource, Conf), NewResource = filename:join([filename:dirname(BasicResource), Filename]), openflax.log:write("new resource: ~p", [NewResource]), Conf0 = openflax.conf:put_value(sreq_basic_resource, NewResource, Conf), {openflax.handler.file, Conf0}. pick(Dir, Files, Conf) -> PickedFile = lists:nth(random:uniform(length(Files)), Files), Filename = filename:join([Dir, PickedFile]), case openflax.handler.file:is_accessible(Filename, Conf) of true -> PickedFile; false -> pick(Dir, Files -- [PickedFile], Conf) end. %%% END of openflax/handler/random.erl %%%