示例#1
0
 /**
  * Constructor.
  * @return void
  */
 public function init()
 {
     if (!isset($this->attrs->server['FR_PATH'])) {
         $this->status(404);
         $this->finish();
         return;
     }
     $req = $this;
     $job = new ComplexJob(function ($job) use($req) {
         $req->wakeup();
     });
     $this->job = $job;
     $this->sleep(1, true);
     $req->attrs->server['FR_PATH'] = FS::sanitizePath($req->attrs->server['FR_PATH']);
     $job('stat', function ($name, $job) use($req) {
         FS::stat($req->attrs->server['FR_PATH'], function ($path, $stat) use($job, $req) {
             if ($stat === -1) {
                 $req->fileNotFound();
                 $job->setResult('stat', false);
                 return;
             }
             if ($stat['type'] === 'd') {
                 $job('readdir', function ($name, $job) use($path, $req) {
                     FS::readdir(rtrim($path, '/'), function ($path, $dir) use($job, $req) {
                         $found = false;
                         if (is_array($dir)) {
                             foreach ($dir['dents'] as $file) {
                                 if ($file['type'] === EIO_DT_REG) {
                                     // is file
                                     if (in_array($file['name'], $req->appInstance->indexFiles)) {
                                         $req->file($path . '/' . $file['name']);
                                         $found = true;
                                         break;
                                     }
                                 }
                             }
                         }
                         if (!$found) {
                             if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) {
                                 $this->autoindex($dir);
                             } else {
                                 $req->fileNotFound();
                             }
                         }
                         $job->setResult('readdir');
                     }, EIO_READDIR_STAT_ORDER | EIO_READDIR_DENTS);
                 });
             } elseif ($stat['type'] == 'f') {
                 $this->file($path);
             }
             $job->setResult('stat', $stat);
         });
     });
     $job();
 }