public function setFromValidatedFile(sfValidatedFile $obj) { $this->setType($obj->getType()); $this->setOriginalFilename($obj->getOriginalName()); $this->setName(strtr($obj->generateFilename(), '.', '_')); $bin = new FileBin(); $bin->setBin(file_get_contents($obj->getTempName())); $this->setFileBin($bin); }
public function executePost(sfWebRequest $request) { $this->forward400If('' === (string) $request['title'], 'title parameter is not specified.'); $this->forward400If('' === (string) $request['body'], 'body parameter is not specified.'); $this->forward400If(!isset($request['public_flag']) || '' === (string) $request['public_flag'], 'public flag is not specified'); if (isset($request['id']) && '' !== $request['id']) { $diary = Doctrine::getTable('Diary')->findOneById($request['id']); $this->forward400If(false === $diary, 'the specified diary does not exit.'); $this->forward400If(false === $diary->isAuthor($this->member->getId()), 'this diary is not yours.'); } else { $diary = new Diary(); $diary->setMemberId($this->member->getId()); } $diary->setTitle($request['title']); $diary->setBody($request['body']); $diary->setPublicFlag($request['public_flag']); $diary->save(); $this->diary = $diary; for ($i = 1; $i <= 3; $i++) { $diaryImage = Doctrine::getTable('DiaryImage')->retrieveByDiaryIdAndNumber($diary->getId(), $i); $filename = basename($_FILES['diary_photo_' . $i]['name']); if (!is_null($filename) && '' !== $filename) { try { $validator = new opValidatorImageFile(array('required' => false)); $validFile = $validator->clean($_FILES['diary_photo_' . $i]); } catch (Exception $e) { $this->forward400($e->getMessage()); } $f = new File(); $f->setFromValidatedFile($validFile); $f->setName(hash('md5', uniqid((string) $i) . $filename)); if ($stream = fopen($_FILES['diary_photo_' . $i]['tmp_name'], 'r')) { if (!is_null($diaryImage)) { $diaryImage->delete(); } $bin = new FileBin(); $bin->setBin(stream_get_contents($stream)); $f->setFileBin($bin); $f->save(); $di = new DiaryImage(); $di->setDiaryId($diary->getId()); $di->setFileId($f->getId()); $di->setNumber($i); $di->save(); $diary->updateHasImages(); } else { $this->forward400(__('Failed to write file to disk.')); } } $deleteCheck = $request['diary_photo_' . $i . '_photo_delete']; if ('on' === $deleteCheck && !is_null($diaryImage)) { $diaryImage->delete(); } } }
public function savePchFileFromRawData(&$pchData, Doctrine_Connection $conn = null) { if ($this->getPchFileId()) { $pchFile = $this->getPchFile(); } else { $pchFile = new File(); } $pchFile->setType('image/pch'); $pchFile->setName('cccc_' . time() . '_pch'); $pchFile->save($conn); $fileBin = $pchFile->getFileBin(); if (!$fileBin) { $fileBin = new FileBin(); $fileBin->setFileId($pchFile->getId); } $fileBin->setBin($pchData); $fileBin->save($conn); $this->setPchFileId($pchFile->getId()); }
public function executeUpload(sfWebRequest $request) { // for apiKey check $memberId = $this->getUser()->getMember(); if ('1' === $request->getParameter('forceHtml')) { // workaround for some browsers $this->getResponse()->setContentType('text/html'); } if (!$_FILES) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } if (!$_FILES['upfile']) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } $filename = basename($_FILES['upfile']['name']); if (!$filename) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } $tmpFileName = $request->getParameter('changedname'); if ($filename != $tmpFileName) { $separates = $separates = explode('.', $filename); $cnt = count($separates); $fname = ''; $ext = ''; if (1 == $cnt) { $fname = $value; } else { $fname = join('', array_slice($separates, 0, $cnt - 1)); $ext = '.' . $separates[$cnt - 1]; } if ('' == $fname) { $filename = $tmpFileName; } } $filename = preg_replace('/\\|\\/|\\*|:|\\?|\\&|\'|\\"|>|<|undefined|\\|/', '-', urldecode($filename)); $communityId = (int) $request->getParameter('community_id'); if (1 <= (int) $communityId) { $community = Doctrine::getTable('Community')->find($communityId); if (!$community->isPrivilegeBelong($this->getUser()->getMember()->getId())) { return $this->renderJSON(array('status' => 'error', 'message' => 'you are not this community member.')); } $dirname = '/c' . $communityId; } else { $dirname = '/m' . $this->getUser()->getMember()->getId(); } //validate $filepath if (!preg_match('/^\\/[mc][0-9]+/', $dirname)) { return $this->renderJSON(array('status' => 'error', 'message' => 'file path error. ' . $dirname)); } $f = new File(); $f->setOriginalFilename($filename); $f->setType($_FILES['upfile']['type']); $f->setName($dirname . '/' . time() . $filename); $f->setFilesize($_FILES['upfile']['size']); if ($stream = fopen($_FILES['upfile']['tmp_name'], 'r')) { $bin = new FileBin(); $bin->setBin(stream_get_contents($stream)); $f->setFileBin($bin); $f->save(); $response = true; } else { //file open error $response = false; } if (true === $response) { return $this->renderJSON(array('status' => 'success', 'message' => 'file up success ' . $response, 'file' => $f->toArray(false))); } else { return $this->renderJSON(array('status' => 'error', 'message' => 'file upload error')); } }
public function createActivityImageByFileInfoAndActivityId(array $fileInfo, $activityId) { $file = new File(); $file->setOriginalFilename(basename($fileInfo['name'])); $file->setType($fileInfo['type']); $fileFormat = $file->getImageFormat(); if (is_null($fileFormat) || '' == $fileFormat) { $fileFormat = pathinfo($fileInfo['name'], PATHINFO_EXTENSION); } $fileBaseName = md5(time()) . '_' . $fileFormat; $filename = 'ac_' . $fileInfo['member_id'] . '_' . $fileBaseName; $file->setName($filename); $file->setFilesize($fileInfo['size']); $bin = new FileBin(); $bin->setBin($fileInfo['binary']); $file->setFileBin($bin); $file->save(); $activityImage = new ActivityImage(); $activityImage->setActivityDataId($activityId); $activityImage->setFileId($file->getId()); $activityImage->setUri($this->getActivityImageUriByfileInfoAndFilename($fileInfo, $filename)); $activityImage->setMimeType($file->type); $activityImage->save(); $this->createUploadImageFileByFileInfoAndSaveFileName($fileInfo, $filename); return $activityImage; }
protected function getImportedPluginFile($filename, $filepath) { $file = new File(); $file->setType('application/x-gzip'); $file->setOriginalFilename($filename); $file->setName(strtr($filename, '.', '_')); $bin = new FileBin(); $bin->setBin(file_get_contents($filepath)); $file->setFileBin($bin); $file->save(); $this->uploadToS3($file); return $file; }