public function put($path, $name, $size, $extension, $mimeType) { /** @var InputFile $fileInfo */ $fileInfo = new InputFile($path, $name, $size, $extension, $mimeType); $fid = UidGenerator::id($fileInfo->checksum); // lock the file while uploading if (!$this->locker->lock($fid)) { FileException::pop('Fail to lock file. Try later.'); } $fileInfo->id($fid); $fileMeta = new FileMeta(); $fileContainer = new FileContainer(); $fileContainer->addHandler(new StorageNodeHandler()); $fileContainer->addHandler(new NodeMetaHandler()); $fileMeta->setFileContainer($fileContainer); if ($sourceFileInfo = $fileMeta->checkExist($fileInfo)) { // make the upload file a soft link // which refers to the source file // lock the source file first to prevent from being deleted if (!$this->locker->lock($sourceFileInfo->id)) { FileException::pop('Fail to lock file. Try later.'); } $fileMeta->softLink($fileInfo, $sourceFileInfo); $this->locker->unlock($sourceFileInfo->id); } else { $fileInfo->nodeId = (new StorageNodeSelector())->getNodeId(); $fileMeta->add($fileInfo); } $fileContainer->dump(); $this->locker->unlock($fid); return $fileInfo->id; }
/** * @param $fid * @return InputFile */ public function decrFileRefCountById($fid) { /** @var static $row */ $row = $this->model->ofId($fid); $row->refCount--; $fileInfo = InputFile::initWithFileMeta($row); if ($row->refCount == 0) { $this->model->deleteById($row->id); } else { $this->model->updateOne($row->id, ['refCount' => $row->refCount]); } return $fileInfo; }