file_manager.anubis 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

 *Project*                             The Anubis Project
   
 *Title*                          A Web Oriented File Manager.
   
 *Copyright*                     Copyright (c) Alain Prouté 2005. 


 *Author*       Alain Prouté
   

  
 *Overview* 
   This file defines a datum of type HTML_Off_Form (see 'web/making_a_web_site.anubis' for
   the definition  of this type) which  is a tool for  managing file through  the web. The
   file manager shows the path of the  directory currently managed, a list of all files in
   the directory (or only those which match some mask), with informations like the size of
   the file  or the date  of last  modification.  There are  buttons for changing  the way
   files are displayed, and for climbing into the parent directory.  There is also a field
   for uploading a  new file into the  directory.  For each subdirectory, there  is a link
   for entering this subdirectory.   When one clicks on a file name,  a popup window opens
   containing tool  for managing the file. You  can delete, rename, download,  or move the
   file. If the file is an executable, you can also execute it. If it is an image, you can
   see it.
   
   As explained in 'web/making_a_web_site.anubis', the  page shown to a client is computed
   from a  state (represented  by parameter '$State'  below).  This is  why 'file_manager'
   actually returns  a function computing  the file manager  itself from the state  of the
   client. From this state, the file manager must  also be able to recover the path of the
   directory  to be  managed, and  to change  the  path of  this directory.   This is  why
   'file_manager' takes  such tools  as its  arguments. The same  principles apply  to the
   layout.

read making_a_web_site.anubis   
read common.anubis   
  
public type FileManagerLayout:
   short,      // only file names
   long.       // file names with details (size, date, ...)
   
public define $State -> HTML_Off_Form
   file_manager
     (
       $State -> String                        get_current_directory,
       $State -> FileManagerLayout             get_layout,
     ). 
   
   The  argument function  'change_directory' returns  a 'Maybe($State)'.  It  must return
   'failure' if the client is not allowed to change to the new directory.
   
   Of course, this file manager comes with a list of actions (of type 'Web_Action($State)'
   as defined in 'web/making_a_web_site.anubis').  The function below constructs this list
   of  actions.   It  should  be  used   to  construct  your  web  site  description  (see
   'web_site_description'  in   'web/making_a_web_site.anubis').  All  action   names  are
   prefixed by "filemng_".

public define List(Web_Action($State))
   file_manager_actions
     (  
       String                                  site_directory, 
       $State -> Bool                          allow,
       $State -> String                        get_current_directory,
       ($State,String) -> Maybe($State)        change_current_directory,
       ($State,FileManagerLayout) -> $State    change_layout
     ). 
   
   Since the  file manager  allows to  perform many operations  on the  server, it  may be
   somewhat dangerous. For this reason, it should be integrated only in some HTTPS secured
   administration page of your web site.
   
   
        
   --- That's all for the public part ! --------------------------------------------------
   
   
   
read tools/basis.anubis

   
   
   
   
   *** [] Displaying the subdirectories
   
define List(HTML_Row(HTML_Off_Form))
   subdirectories
     (
       String                cd, 
       List(FileDescription) l, 
       Bool                  b
     ) =
   if l is 
     {
       [ ]      then [ ],
       [h . t]  then if h is 
         {
           no_info(_)          then subdirectories(cd,t,b),
           file(_,_,_,_)       then subdirectories(cd,t,b),
           link(_,_,_,_)       then subdirectories(cd,t,b),
           directory(name,_,_) then 
             [row([/* background_color(if b then rgb(255,240,240) else rgb(230,255,230)) */],
                [
                  cell([],actioner(same,same,
                                   link([size(8)],name),
                                   "filemng_chdir",
                                   [("new",cd+"/"+name)])), 
                ]) 
              . subdirectories(cd,t,not(b))]
         }
     }. 
   
   
define HTML_Off_Form
   subdirectories
     (
       $State                                  s,
       $State -> String                        gcd,
       $State -> FileManagerLayout             gl
     ) =
   with cd = gcd(s), 
   dirs = directory_full_list(cd+"/","","","*"), 
   if dirs is []
   then text([color(rgb(240,0,0))],"No subdirectory.")
   else table([nude],subdirectories(cd,dirs,true)). 
        
   
   
   
   *** [] Displaying the files
   
define String 
   zero_pad_2
     (
       Int n
     ) =
   with s = abs_to_decimal(n), 
   if length(s) < 2
   then "0"+s 
   else s. 
   
     
define String
   format_month
     (
       Int m
     ) =
   if m = 1  then "jan" else
   if m = 2  then "feb" else
   if m = 3  then "mar" else
   if m = 4  then "apr" else
   if m = 5  then "may" else
   if m = 6  then "jun" else
   if m = 7  then "jul" else
   if m = 8  then "aug" else
   if m = 9  then "sep" else
   if m = 10 then "oct" else
   if m = 11 then "nov" else
   if m = 12 then "dec" else
   "???". 
   
   
define String 
   format_date
     (
       Int t
     ) =
   if convert_time(t) is date_and_time(year,month,day,hour,min,sec,wday,yday,dst) then 
   abs_to_decimal(year)+" "+format_month(month)+" "+zero_pad_2(day)+" "+
   zero_pad_2(hour)+":"+zero_pad_2(min)+":"+zero_pad_2(sec). 
 
   
define List(HTML_Row(HTML_Off_Form))
   files
     (
       String                cd, 
       List(FileDescription) l, 
       Bool                  b
     ) =
   if l is 
     {
       [ ]      then [ ],
       [h . t]  then if h is 
         {
           no_info(_)          then files(cd,t,b),
           file(name,size,mode,lm)    then 
             [row([background_color(if b then rgb(255,240,240) else rgb(240,255,240))],
                [
                  /*
                  cell([],actioner(same,
                                   other("edit",[resizable,scrollbars]),
                                   link([color(red),size(8)],"Edit"),
                                   "filemng_edit",
                                   [("name",cd+"/"+name)])),
                  cell([width(8)],text([],"")),
                  */ 
                  cell([width(200)],private_download(cd+"/"+name,name,"")),
                  cell([width(8)],text([],"")),
                  cell([width(40)],text([color(rgb(230,0,0))],to_decimal(size))),
                  cell([width(8)],text([],"")),
                  cell([nowrap],text([],format_date(to_Int(lm))))
                ]) 
              . files(cd,t,not(b))]
           link(_,_,_,_)       then files(cd,t,b),
           directory(_,_,_)    then files(cd,t,b)
         }
     }. 
   
   
define HTML_Off_Form
   files
     (
       $State                                  s,
       $State -> String                        gcd,
       $State -> FileManagerLayout             gl
     ) =
   with cd = gcd(s), 
       fds = directory_full_list(cd+"/","*","",""),
   if fds is [] 
   then text([color(rgb(230,0,0)),size(12)],"No file in this directory.")
   else table([nude],files(cd,fds,true)). 
        
   
   
   
   
   
   
public define $State -> HTML_Off_Form
   file_manager
     (
       $State -> String                        gcd,
       $State -> FileManagerLayout             gl,
     ) =
   ($State s) |-> 
     table([nude],[
   
       //--- displaying the current directory ---
       row(cell([h_center,background_color(rgb(255,255,0))],
                text([size(12),color(rgb(0,0,255))],gcd(s)))),
   
       //--- some buttons ---
       row(cell([left],table([],[row([
         cell([],actioner(same,                //same connection
                          same,                // same window
                          link([bold,font("helvetica"),size(14)],"Up"),
                          "filemng_up",
                          []))
   
           ])]))), 
   
       //--- displaying the subdirectories and files ---
       row(cell([h_center],table([border(0,3,4,rgb(0,0,0))],[
         row([
           cell([h_center],text([size(14)],"Subdirectories")),
           cell([h_center],text([size(14)],"Files"))
             ]),
         row([
   /*
           cell([],fixed_size(absolute(200),absolute(200),center(subdirectories(s,gcd,gl)))),
           cell([],fixed_size(absolute(500),absolute(200),center(         files(s,gcd,gl))))
   */
           cell([top,left],subdirectories(s,gcd,gl)),
           cell([top,left],files(s,gcd,gl))
             ])]))),
   
        //--- upload field ---
        row(cell([h_center],form("",
          table([],[
             row([
               cell([h_center,bottom,height(40)],
                    text([color(rgb(230,0,0))],"Add a new file to this directory")),
               cell([],text([],""))
                 ]),
             row([
               cell([],file_upload("file",80)),
               cell([width(10)],text([],"")),
               cell([],actioner(same,same,
                                link([bold,font("helvetica"),size(14)],"Send"),
                                "filemng_upload",
                                []))
                 ])]))))
      
       ]). 
   
   
   
   *** [] Carve the parent directory from a path. 
   
   We need to find the last '/'. If it does not exist we keep the sale path. If it exists,
   we keep what comes just before it. 
   
   Actually, we search backwards for the first '/' from the end. 
   
define Maybe(Int)
   find_last_slash
     (
       String s,
       Int    i
     ) =
   if nth(i,s) is 
     {
       failure     then failure, // out of range
       success(c)  then 
         if c = '/'
         then success(i)
         else if c = '\\'
              then success(i)
              else find_last_slash(s,i-1)
     }. 
   
define Maybe(Int)
   find_last_slash
     (
       String s
     ) =
   find_last_slash(s,length(s)-1). 
   
define String 
   carve_parent
     (
       String path
     ) =
   if find_last_slash(path) is 
     {
       failure     then  path, 
       success(i)  then  substr(path,0,i)
     }. 
   

define One
   print
     (
       Maybe(Word8) x
     ) =
   if x is 
     {
       failure    then print("--- failure\n"), 
       success(c) then print("--- success("+to_decimal(c)+")\n")
     }.
   
   
public define List(Web_Action($State))
   file_manager_actions
     (  
       String                                  site_dir, 
       $State -> Bool                          allow,
       $State -> String                        gcd,
       ($State,String) -> Maybe($State)        ccd,
       ($State,FileManagerLayout) -> $State    cl
     ) =
   [
   
     //--- going up to the parent directory ---
     https_action("filemng_up",
                  allow, 
                  (List(Web_arg) lwa,
                   $State        s) |-> 
                      if ccd(s,carve_parent(gcd(s))) is 
                        {
                          failure         then s,
                          success(s1)     then s1
                        }),
   
     //--- changing the layout to 'short' ---
     https_action("filemng_short", 
                  allow, 
                  (List(Web_arg) l, 
                   $State        s) |-> cl(s,short)),
   
     //--- changing the layout to 'long' ---
     https_action("filemng_short", 
                  allow, 
                  (List(Web_arg) l, 
                   $State        s) |-> cl(s,long)),
   
     //--- changing to a subdirectory ---
     https_action("filemng_chdir", 
                  allow, 
                  (List(Web_arg) l, 
                   $State        s) |-> 
                     if web_arg_value(l,"new") is 
                       {
                         not_found  then s,
                         found(new) then if ccd(s,new) is 
                           {
                             failure     then s, 
                             success(s1) then s1
                           }
                       }),
   
     //--- uploading a file ---
     https_action("filemng_upload",
                  allow,
                  (List(Web_arg) l, 
                   $State        s) |-> 
                     if file_upload_value(l,"file") is 
                       {
                         failure    then s,
                         success(p) then if p is (fn,tfn) then 
                           forget(execute("cp",[tfn,gcd(s)+"/"+fn])); s
                       })
                        

   /*
     //--- Editing a file ---
     https_action("filemng_edit",
                  allow, 
                  (List(Web_arg) lwa, 
                   State         s) |-> 
                     if web_arg_value(lwa,"name") is 
                       {
                         not_found    then s,
                         found(path)  then 
                       })
   */             
   
   ].