/** * Main * * Exitcodes used: * <ul> * <li>127: Archive referenced in -xar [...] does not exist</li> * <li>126: No manifest or manifest does not have a main-class</li> * </ul> * * @see http://tldp.org/LDP/abs/html/exitcodes.html * @param string[] args * @return int */ public static function main(array $args) { // Open archive $f = new File(array_shift($args)); if (!$f->exists()) { Console::$err->writeLine('*** Cannot find archive ' . $f->getURI()); return 127; } // Register class loader $cl = \lang\ClassLoader::registerLoader(new \lang\archive\ArchiveClassLoader(new Archive($f))); if (!$cl->providesResource(self::MANIFEST)) { Console::$err->writeLine('*** Archive ' . $f->getURI() . ' does not have a manifest'); return 126; } // Load manifest $pr = Properties::fromString($cl->getResource(self::MANIFEST)); if (null === ($class = $pr->readString('archive', 'main-class', null))) { Console::$err->writeLine('*** Archive ' . $f->getURI() . '\'s manifest does not have a main class'); return 126; } // Run main() try { return \lang\XPClass::forName($class, $cl)->getMethod('main')->invoke(null, [$args]); } catch (\lang\reflect\TargetInvocationException $e) { throw $e->getCause(); } }
/** * Handle a single request * * @param string method request method * @param string query query string * @param [:string] headers request headers * @param string data post data * @param peer.Socket socket * @return int */ public function handleRequest($method, $query, array $headers, $data, Socket $socket) { $url = parse_url($query); $f = new File($this->docroot, strtr(preg_replace('#\\.\\./?#', '/', urldecode($url['path'])), '/', DIRECTORY_SEPARATOR)); if (!is_file($f->getURI())) { return call_user_func($this->notFound, $this, $socket, $url['path']); } // Implement If-Modified-Since/304 Not modified $lastModified = $f->lastModified(); if ($mod = $this->header($headers, 'If-Modified-Since')) { $d = strtotime($mod); if ($lastModified <= $d) { $this->sendHeader($socket, 304, 'Not modified', []); return 304; } } clearstatcache(); try { $f->open(File::READ); } catch (IOException $e) { $this->sendErrorMessage($socket, 500, 'Internal server error', $e->getMessage()); $f->close(); return 500; } // Send OK header and data in 8192 byte chunks $this->sendHeader($socket, 200, 'OK', ['Last-Modified: ' . gmdate('D, d M Y H:i:s T', $lastModified), 'Content-Type: ' . MimeType::getByFileName($f->getFilename()), 'Content-Length: ' . $f->size()]); while (!$f->eof()) { $socket->write($f->read(8192)); } $f->close(); return 200; }
/** * Constructor * * @param io.File file * @throws lang.IllegalArgumentException if the given file does not exist */ public function __construct(File $file) { if (!$file->exists()) { throw new IllegalArgumentException('File "' . $file->getURI() . '" does not exist!'); } $this->file = $file; }
/** * Read from a file * * @deprecated Use img.io.MetaDataReader instead * @param io.File file * @param var default default void what should be returned in case no data is found * @return img.util.IptcData * @throws lang.FormatException in case malformed meta data is encountered * @throws lang.ElementNotFoundException in case no meta data is available * @throws img.ImagingException in case reading meta data fails */ public static function fromFile(\io\File $file) { if (false === getimagesize($file->getURI(), $info)) { $e = new ImagingException('Cannot read image information from ' . $file->getURI()); \xp::gc(__FILE__); throw $e; } if (!isset($info['APP13'])) { if (func_num_args() > 1) { return func_get_arg(1); } throw new ElementNotFoundException('Cannot get IPTC information from ' . $file->getURI() . ' (no APP13 marker)'); } if (!($iptc = iptcparse($info['APP13']))) { throw new \lang\FormatException('Cannot parse IPTC information from ' . $file->getURI()); } // Parse creation date if (3 == sscanf(@$iptc['2#055'][0], '%4d%2d%d', $year, $month, $day)) { $created = Date::create($year, $month, $day, 0, 0, 0); } else { $created = null; } with($i = new self()); $i->setTitle(@$iptc['2#005'][0]); $i->setUrgency(@$iptc['2#010'][0]); $i->setCategory(@$iptc['2#015'][0]); $i->setSupplementalCategories(@$iptc['2#020']); $i->setKeywords(@$iptc['2#025']); $i->setSpecialInstructions(@$iptc['2#040'][0]); $i->setDateCreated($created); $i->setAuthor(@$iptc['2#080'][0]); $i->setAuthorPosition(@$iptc['2#085'][0]); $i->setCity(@$iptc['2#090'][0]); $i->setState(@$iptc['2#095'][0]); $i->setCountry(@$iptc['2#101'][0]); $i->setOriginalTransmissionReference(@$iptc['2#103'][0]); $i->setHeadline(@$iptc['2#105'][0]); $i->setCredit(@$iptc['2#110'][0]); $i->setSource(@$iptc['2#115'][0]); $i->setCopyrightNotice(@$iptc['2#116'][0]); $i->setCaption(@$iptc['2#120'][0]); $i->setWriter(@$iptc['2#122'][0]); return $i; }
/** * Constructor * * @param io.File file * @throws lang.IllegalArgumentException if the given file does not exist */ public function __construct(File $file) { $uri = $file->getURI(); $cl = ClassLoader::getDefault()->findUri($uri); if ($cl === null) { throw new IllegalArgumentException('File "' . $uri . ($file->exists() ? '" is not in class path' : '" not found')); } $this->loader = $cl; $this->uri = $uri; }
public function pathClassCanBeUsedAsArg() { $fn = $this->fileKnownToExist(); $f = new File(new Path($fn)); $this->assertEquals($fn, $f->getURI()); }
public function rooted_file() { $rooted = new File('/rooted.ext'); $this->assertEquals($rooted->getURI(), (new Path($rooted))->toString()); }
/** * Assertion helper * * @param string expected * @return io.File target * @throws unittest.AssertionFailedError */ private function assertTarget($expected, File $target) { $this->assertEquals((new File(strtr($expected, '/', DIRECTORY_SEPARATOR)))->getURI(), str_replace(rtrim(realpath('.'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, '', $target->getURI())); }
public function moving() { with($data = 'Hello World'); $this->writeData($this->file, $data); $target = new File($this->file->getURI() . '.moved'); $this->file->move($target->getURI()); $read = $this->readData($target); $target->unlink(); $this->assertEquals($data, $read); // FIXME I don't think io.File should be updating its URI when // move() is called. Because it does, this assertion fails! // $this->assertFalse($this->file->exists()); }
/** * Create a property file from an io.File object * * @deprecated Use load() method instead * @param io.File file * @return util.Properties * @throws io.IOException in case the file given does not exist */ public static function fromFile(File $file) { $self = new self($file->getURI()); $self->load($file->getInputStream()); return $self; }
/** * Read from a file * * @deprecated Use img.io.MetaDataReader instead * @param io.File file * @param var default default void what should be returned in case no data is found * @return img.util.ExifData * @throws lang.FormatException in case malformed meta data is encountered * @throws lang.ElementNotFoundException in case no meta data is available * @throws img.ImagingException in case reading meta data fails */ public static function fromFile(\io\File $file) { if (false === getimagesize($file->getURI(), $info)) { $e = new ImagingException('Cannot read image information from ' . $file->getURI()); \xp::gc(__FILE__); throw $e; } if (!isset($info['APP1'])) { if (func_num_args() > 1) { return func_get_arg(1); } throw new ElementNotFoundException('Cannot get EXIF information from ' . $file->getURI() . ' (no APP1 marker)'); } if (!($info = exif_read_data($file->getURI(), 'COMPUTED,FILE,IFD0,EXIF,COMMENT,MAKERNOTE', true, false))) { throw new \lang\FormatException('Cannot get EXIF information from ' . $file->getURI()); } // Change key case for lookups foreach ($info as &$val) { $val = array_change_key_case($val, CASE_LOWER); } with($e = new self()); // COMPUTED info $e->setWidth(self::lookup($info['COMPUTED'], 'width')); $e->setHeight(self::lookup($info['COMPUTED'], 'height')); $e->setApertureFNumber(self::lookup($info['COMPUTED'], 'aperturefnumber')); // IFD0 info $e->setMake(trim(self::lookup($info['IFD0'], 'make'))); $e->setModel(trim(self::lookup($info['IFD0'], 'model'))); $software = self::lookup($info['IFD0'], 'software'); $e->setSoftware(null === $software ? null : trim($software)); if (null !== ($o = self::lookup($info['IFD0'], 'orientation'))) { $e->setOrientation($o); } else { $e->setOrientation($e->width / $e->height > 1.0 ? 1 : 5); } // FILE info $e->setFileName(self::lookup($info['FILE'], 'filename')); $e->setFileSize(self::lookup($info['FILE'], 'filesize')); $e->setMimeType(self::lookup($info['FILE'], 'mimetype')); // EXIF info $e->setExposureTime(self::lookup($info['EXIF'], 'exposuretime')); $e->setExposureProgram(self::lookup($info['EXIF'], 'exposureprogram')); $e->setMeteringMode(self::lookup($info['EXIF'], 'meteringmode')); $e->setIsoSpeedRatings(self::lookup($info['EXIF'], 'isospeedratings')); // Sometimes white balance is in MAKERNOTE - e.g. FUJIFILM's Finepix if (null !== ($w = self::lookup($info['EXIF'], 'whitebalance'))) { $e->setWhiteBalance($w); } else { if (isset($info['MAKERNOTE']) && null !== ($w = self::lookup($info['MAKERNOTE'], 'whitebalance'))) { $e->setWhiteBalance($w); } else { $e->setWhiteBalance(null); } } // Extract focal length. Some models store "80" as "80/1", rip off // the divisor "1" in this case. if (null !== ($l = self::lookup($info['EXIF'], 'focallength'))) { sscanf($l, '%d/%d', $n, $frac); $e->setFocalLength(1 == $frac ? $n : $n . '/' . $frac); } else { $e->setFocalLength(null); } // Check for Flash and flashUsed keys if (null !== ($f = self::lookup($info['EXIF'], 'flash'))) { $e->setFlash($f); } else { $e->setFlash(null); } if (null !== ($date = self::lookup($info['EXIF'], 'datetimeoriginal', 'datetimedigitized'))) { $t = sscanf($date, '%4d:%2d:%2d %2d:%2d:%2d'); $e->setDateTime(new Date(mktime($t[3], $t[4], $t[5], $t[1], $t[2], $t[0]))); } return $e; }