function testCreateFileDoNotSetReleaseTimeIfAlreadySet() { $p = new MockProject($this); $p->setReturnValue('getUnixName', 'prj'); $r = new FRSRelease(); $r->setReleaseID(456); $r->setPackageID(123); $r->setGroupID(1113); $r->setProject($p); $f = new FRSFile(); $f->setRelease($r); $f->setGroup($p); $f->setFileName('file_sample'); $f->setReleaseTime(3125); $f->setPostDate(3125); $dao = new MockFRSFileDao($this); stub($dao)->searchFileByName()->returnsEmptyDar(); $ff = new FRSFileFactoryFakeCreation(); $ff->setReturnValue('create', 55); $ff->setReturnValue('moveFileForge', True); $ff->dao = $dao; $rf = new MockFRSReleaseFactory($this); $rf->setReturnValue('getFRSReleaseFromDb', $r); $ff->release_factory = $rf; $before = time(); $ff->createFile($f); $after = time(); $this->assertTrue($f->getPostDate() == 3125); $this->assertTrue($f->getReleaseTime() == 3125); }
/** * Create a new file based on given objects * * Given a "transient" file object, physically move the file from it's landing zone to * it's release area and create the corresponding entry in the database. * * @param FRSFile $file File to create * * @return FRSFile */ public function createFile(FRSFile $file, $extraFlags = self::COMPUTE_MD5) { $rule = new Rule_FRSFileName(); if (!$rule->isValid($file->getFileName())) { throw new FRSFileIllegalNameException($file); } $rel = $file->getRelease(); if ($this->isFileBaseNameExists($file->getFileName(), $rel->getReleaseID(), $rel->getGroupID())) { throw new FRSFileExistsException($file); } if ($this->isSameFileMarkedToBeRestored($file->getFileName(), $rel->getReleaseID(), $rel->getGroupID())) { throw new FRSFileToBeRestoredException($file); } clearstatcache(); $filePath = $this->getSrcDir($rel->getProject()) . '/' . $file->getFileName(); if (!file_exists($filePath)) { throw new FRSFileInvalidNameException($file); } if (0 != ($extraFlags & self::COMPUTE_MD5)) { $file->setComputedMd5(PHP_BigFile::getMd5Sum($filePath)); if (!$this->compareMd5Checksums($file->getComputedMd5(), $file->getReferenceMd5())) { throw new FRSFileMD5SumException($file); } } $file->setFileSize(PHP_BigFile::getSize($filePath)); $file->setStatus('A'); $now = time(); if ($file->getReleaseTime() === null) { $file->setReleaseTime($now); } if ($file->getPostDate() === null) { $file->setPostDate($now); } if ($this->moveFileForge($file)) { $fileId = $this->create($file->toArray()); if ($fileId) { $file->setFileID($fileId); return $file; } else { throw new FRSFileDbException($file); } } else { throw new FRSFileForgeException($file); } }
private function importFile(Project $project, FRSRelease $release, PFUser $user, SimpleXMLElement $xml_file, $extraction_path) { $user = empty($xml_file->user) ? $user : $this->user_finder->getUser($xml_file->user); $attrs = $xml_file->attributes(); $src = $extraction_path . '/' . $attrs['src']; $name = isset($attrs['name']) ? (string) $attrs['name'] : basename($src); $md5 = strtolower(md5_file($src)); $time = strtotime($attrs['release-time']); $date = strtotime($attrs['post-date']); $desc = ""; $type_id = $this->getFileTypeDao()->searchTypeId($attrs['filetype']); if (is_null($type_id)) { throw new Exception("Invalid filetype '{$attrs['filetype']}'"); } $proc_id = $this->getProcessorDao()->searchProcessorId($project->getID(), $attrs['arch']); if (is_null($proc_id)) { throw new Exception("Invalid architecture '{$attrs['arch']}'"); } foreach ($xml_file->children() as $elem) { if ($elem->getName() != "description") { continue; } $desc .= (string) $elem; } if (isset($attrs['md5sum'])) { $expected_md5 = strtolower($attrs['md5sum']); if ($expected_md5 != $md5) { throw new Exception("Import of file {$src} failed because the file is corrupted " . "(expected MD5 {$expected_md5}, got {$md5})"); } } $dirPath = $this->file_factory->getSrcDir($project); $dest = "{$dirPath}/{$name}"; if (!copy($src, $dest)) { throw new Exception("Could not copy {$src} to {$dest}"); } $newFile = new FRSFile(); $newFile->setGroup($project); $newFile->setRelease($release); $newFile->setFileName($name); $newFile->setProcessorID($proc_id); $newFile->setTypeID($type_id); $newFile->setReferenceMd5($md5); $newFile->setComputedMd5($md5); $newFile->setUserId($user->getId()); $newFile->setComment($desc); $newFile->setReleaseTime($time); $newFile->setPostDate($date); $this->file_factory->createFile($newFile); }