SHARE
TWEET

Untitled

a guest Jul 18th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5.  
  6. use Carbon\Carbon;
  7. use App\Models\Format;
  8. use App\Models\Company;
  9. use App\Shared\Logger;
  10. use App\Shared\Mailer;
  11. use App\Shared\UserUtils;
  12. use Illuminate\Http\Request;
  13. use App\Formatters\Formatter;
  14. use App\Models\Transformation;
  15. use Illuminate\Routing\Controller;
  16. use Illuminate\Support\Facades\Storage;
  17. use Illuminate\Support\Facades\Mail;
  18. use File;
  19.  
  20. class UserFormatController extends Controller
  21. {
  22.  
  23.     public function tranformForm(Request $request, $id)
  24.     {
  25.  
  26.         $format = Format::with('format_subscription')->find($id);
  27.         return view('format.userForm')->with(['format' => $format]);
  28.     }
  29.  
  30.     public function uploadFiles(Request $request)
  31.     {
  32.  
  33.         if ($request->ajax()) {
  34.             $format = Format::find($request->id);
  35.             $formatter = $format->buildFormatter();
  36.  
  37.             $afterslug = $formatter->getAfterSlug();
  38.             $inputFile = $request->file('file');
  39.  
  40.             $filename = date('Ymd-His') . '-format' . str_pad($request->id, 4, '0', STR_PAD_LEFT) . '.' . $inputFile->getClientOriginalName();
  41.             $inputFilepath = storage_path('input_files' . config('app.fileSystemSeparator') . $format->slug . config('app.fileSystemSeparator') . $formatter->getAfterSlug());
  42.  
  43.             Logger::info(__METHOD__, "Saving input file " . ($filename) . '...');
  44.  
  45.             $inputRealpath = $inputFilepath . $filename;
  46.  
  47.             $inputFile->move($inputFilepath, $filename);
  48.  
  49.             return response()->json([
  50.                 'success' => true,
  51.                 'filename' => $filename,
  52.             ], 200);
  53.         }
  54.     }
  55.  
  56.     public function doTransform(Request $request, $id)
  57.     {
  58.         $format = Format::find($request->id);
  59.         $formatter = $format->buildFormatter();
  60.  
  61.         $filenames = json_decode($request->filenames);
  62.  
  63.         if (count($filenames) > 1) {
  64.  
  65.             foreach ($filenames as  $k => $v) {
  66.                 $inputFilepath[$k] = storage_path('input_files' . config('app.fileSystemSeparator') . $format->slug . config('app.fileSystemSeparator') . $formatter->getAfterSlug());
  67.                 $inputRealpath[$k] = $inputFilepath[$k] . config('app.fileSystemSeparator') . $filenames[$k];
  68.  
  69.                 $formatter->addInputFile($inputRealpath[$k]);
  70.                 $formatter->setOutputPath(storage_path('output_files' . config('app.fileSystemSeparator')));
  71.  
  72.                 $outputFilename = $formatter->getOutputFilename();
  73.                 $path = storage_path('output_files' . config('app.fileSystemSeparator'));
  74.                 $outputFilepath =  $path . $format->slug . config('app.fileSystemSeparator') . $formatter->getAfterSlug() . config('app.fileSystemSeparator') . $outputFilename;
  75.                 $outputRealFilepath = $outputFilepath;
  76.             }
  77.         } else {
  78.             $inputFilepath = storage_path('input_files' . config('app.fileSystemSeparator') . $format->slug . config('app.fileSystemSeparator') . $formatter->getAfterSlug());
  79.  
  80.             $inputRealpath = $inputFilepath . config('app.fileSystemSeparator') . $filenames[0];
  81.             $formatter->addInputFile($inputRealpath);
  82.             $formatter->setOutputPath(storage_path('output_files' . config('app.fileSystemSeparator')));
  83.  
  84.             $outputFilename = $formatter->getOutputFilename();
  85.             $path = storage_path('output_files');
  86.             $outputFilepath = $path . config('app.fileSystemSeparator') . $format->slug . config('app.fileSystemSeparator') . $outputFilename;
  87.             $outputRealFilepath = $outputFilepath;
  88.         }
  89.  
  90.         if (count($filenames) >= 2) {
  91.  
  92.             $string_resultData = $formatter->format();
  93.             $resultData = explode('\n', $string_resultData);
  94.             $outputFile = $formatter->writeOutput($resultData, $outputRealFilepath);
  95.         } else if (count($filenames) == 1) {
  96.             $resultData = $formatter->format();
  97.             $outputFile = $formatter->writeOutput($resultData, $outputRealFilepath);
  98.         }
  99.  
  100.  
  101.  
  102.         Logger::info(__METHOD__, "Saving output file " . ($outputRealFilepath) . '...');
  103.  
  104.         $data = [
  105.             'format_id' => $format->id,
  106.             'company_id' => UserUtils::getCompanyID(),
  107.             'transformation_time' => Carbon::now(),
  108.             'error' => '',
  109.             'input_files' => json_encode($inputRealpath),
  110.             'output_files' => json_encode($outputRealFilepath),
  111.             'notes' => $request->notes,
  112.         ];
  113.  
  114.         $transform = Transformation::create($data);
  115.         $mailer = new Mailer();
  116.         $company = Company::find($format->company_id);
  117.         $with = [
  118.             'company_name' => $company->company_name
  119.         ];
  120.  
  121.         $attach = $mailer->getAttachFromStorage($outputRealFilepath, $outputFilename);
  122.  
  123.         if (!is_null($attach)) {
  124.             $viewEmail = "emails.test";
  125.             $subjectEmail = "Your File is Converted";
  126.             $result = $mailer->sendMail($viewEmail, $company, $subjectEmail, $with, $attach);
  127.         }
  128.  
  129.         return response()->download($outputRealFilepath);
  130.     }
  131.  
  132.     public function transformed(Request $request)
  133.     {
  134.         return view('format.userTransformed');
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top