public function storeFile($directory, $uploadedFile) { Storage::makeDirectory($directory); if (!$uploadedFile) { return false; } do { $this->file_name = str_random(40); } while (UploadedFile::where('file_name', $this->file_name)->count() > 0); $this->folder = $directory; $this->original_file_name = $uploadedFile->getClientOriginalName(); $uploadedFile->move(storage_path() . $directory, $this->file_name); $this->mime_type = File::mimeType(storage_path() . $directory . $this->file_name); $this->save(); return true; }
/** * Bootstrap any application services. * * @return void */ public function boot() { view()->composer('frontend.layout.header', function ($view) { $view->with('user', Auth::user()); }); view()->composer('frontend.partials.domainsTree', function ($view) { $domains = Domain::getCurrentDomains(); $tree = Domain::getTree(Domain::getDomainsForTree($domains)); $view->with(['tree' => $tree, 'domains' => $tree[1]['subdomains']]); }); view()->composer('frontend.partials.domainsTreeForReports', function ($view) { $domains = Domain::getCurrentDomains(); $tree = Domain::getTree(Domain::getDomainsForTree($domains)); $view->with(['tree' => $tree, 'domains' => $tree[1]['subdomains']]); }); Document::deleted(function ($document) { $fileToDelete = UploadedFile::where('id', '=', $document['uploaded_file_id'])->get(); $fileToDelete = $fileToDelete[0]['file_name']; File::delete(storage_path() . '/documents/' . $fileToDelete); $document->file()->delete(); return true; }); }
public function downloadFile($fileName) { $entry = UploadedFile::where('file_name', $fileName)->firstOrFail(); return response()->download(storage_path() . $entry->folder . $entry->file_name, $entry->original_file_name); }
protected function importRemainingDocuments() { $initDocuments = Document::count(); $lostDocuments = []; $documentsForExtendedIssues = []; $pathToFiles = sprintf('%s/var/www/andr_v2/uploads/reldocs/propid_/stepid_', storage_path()); $dbPathToFile = '/var/www/andr_v2/uploads/reldocs/propid_/stepid_/'; $oldDocuments = DB::connection('oldissue')->select('select * from relateddoc where propid > 120 and stepid <> 0 '); $getFileNamesFromFolder = array_diff(scandir($pathToFiles), ['.', '..']); foreach ($oldDocuments as $document) { if (!$document->filepaths) { continue; } if (strpos($document->filepaths, $dbPathToFile) === 0) { $lostDocuments[] = $document; } } foreach ($lostDocuments as $document) { $fullPathToFile = $document->filepaths; $file = str_replace($dbPathToFile, '', $fullPathToFile); if (!in_array($file, $getFileNamesFromFolder)) { continue; } do { $randomName = str_random(40); } while (UploadedFile::where('file_name', $randomName)->count() > 0); do { $codPublic = str_random(40); } while (Document::where('public_code', $codPublic)->count() > 0); $uploadedFileData = ['file_name' => $randomName, 'folder' => '/documents/', 'original_file_name' => $file]; $this->moveFile(storage_path() . $fullPathToFile, $uploadedFileData['file_name']); $doc = factory(Document::class)->create(['public' => 1, 'uploaded_file_id' => factory(UploadedFile::class)->create($uploadedFileData)->id, 'public_code' => $codPublic, 'init_at' => $document->initat]); $translatableData = ['ro' => ['title' => $document->content ? $document->content : ''], 'en' => ['title' => $document->encontent ? $document->encontent : '']]; $doc->fill($translatableData); $doc->save(); try { $doc->steps()->attach($document->stepid); } catch (\Exception $e) { print_r("Shiit! O relatie Document - FlowStep nu s-a putut importa.\n"); } } echo sprintf("Au fost recuperate %s documente salvate la comun intr-un folder.\n", $count = Document::count() - $initDocuments); return true; }