File.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Helpers;
  3. use App\Exceptions\ParameterException;
  4. use App\Services\CallApiService;
  5. use App\Services\MediaLibraryService;
  6. use Exception;
  7. use Illuminate\Filesystem\Filesystem;
  8. class File
  9. {
  10. public static function codeTitleFor($type, $name, $value)
  11. {
  12. return self::getCodeTitleFiles($type)[$name][$value]['Title'];
  13. }
  14. public static function mimeType($path)
  15. {
  16. return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  17. }
  18. public static function uploadImg($file)
  19. {
  20. $mediaLibraryService = app(MediaLibraryService::class);
  21. $mediaLibraryService->setGateToken(session('GateToken')['main']);
  22. $setup = $mediaLibraryService->getSetup('post');
  23. $path = $mediaLibraryService->getCurrSetupFilePath($setup);
  24. $path = preg_replace('/\/$/', '', $path);
  25. $filePath = \Storage::disk(getDisk())->put($path, $file);
  26. return '/' . $filePath;
  27. }
  28. public static function changeEnvironmentVariable($key, $value, $isArr = false, $fileName = '.env')
  29. {
  30. $path = base_path($fileName);
  31. if (is_bool(env($key)))
  32. {
  33. $old = env($key) ? 'true' : 'false';
  34. }
  35. else if (env($key) === null){
  36. $old = 'null';
  37. }
  38. else {
  39. $old = env($key);
  40. }
  41. if (file_exists($path)) {
  42. if ($isArr) {
  43. $old = "'$old'";
  44. $value = "'$value'";
  45. }
  46. file_put_contents($path, str_replace(
  47. "$key=" . $old, "$key=".$value, file_get_contents($path)
  48. ));
  49. }
  50. }
  51. public static function getSkinDirectories()
  52. {
  53. return array_map('basename', \Storage::disk('erp')->directories('/themes/pro'));
  54. }
  55. public static function getThemeCodeTitleDirectories($themeDir)
  56. {
  57. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  58. $setPath = "themes/{$themeDir}/para/{$countryCode}/etc/code-title";
  59. if (file_exists(daboryPath($setPath))) {
  60. $paramsFilePath = array_map('basename', \Storage::disk('erp')->directories($setPath));
  61. } else {
  62. throw new ParameterException('CodeTitle Directories Not Found (Theme) : ' . daboryPath($setPath));
  63. }
  64. return $paramsFilePath;
  65. }
  66. public static function getThemeCodeTitleFiles($funcName, $themeDir)
  67. {
  68. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  69. $setPath = "themes/{$themeDir}/para/{$countryCode}/etc/code-title/${funcName}";
  70. if (\Storage::disk('erp')->exists($setPath)) {
  71. $files = \Storage::disk('erp')->allFiles($setPath);
  72. foreach ($files as $key => $file) {
  73. $data[basename($file, '.json')] = collect(json_decode(\Storage::disk('erp')->get($file), true))->keyBy('Code')->toArray();
  74. }
  75. }
  76. return $data;
  77. }
  78. public static function getCodeTitleDirectories()
  79. {
  80. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  81. $setPath = "para/erp/{$countryCode}/etc/code-title";
  82. if (file_exists(daboryPath($setPath))) {
  83. $paramsFilePath = array_map('basename', \Storage::disk('dabory')->directories($setPath));
  84. } else {
  85. throw new ParameterException('CodeTitle Directories Not Found (Main) : ' . daboryPath($setPath));
  86. }
  87. return $paramsFilePath;
  88. }
  89. public static function getCodeTitleFiles($funcName)
  90. {
  91. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  92. $setPath = "para/erp/{$countryCode}/etc/code-title/${funcName}";
  93. if (\Storage::disk('dabory')->exists($setPath)) {
  94. $files = \Storage::disk('dabory')->allFiles($setPath);
  95. foreach ($files as $key => $file) {
  96. $data[basename($file, '.json')] = collect(json_decode(\Storage::disk('dabory')->get($file), true))->keyBy('Code')->toArray();
  97. }
  98. }
  99. return $data;
  100. }
  101. // 라라벨에서 파일업로드할 때 php.ini에 정해져있는 임시폴더에 저장하는 방법 비슷하게 구현
  102. public static function createFromUrl(string $url, string $originalName = '', string $mimeType = null, int $error = null, bool $test = false)
  103. {
  104. // url을 이용해서 파일 Read.
  105. if (! $stream = @fopen($url, 'r')) {
  106. throw new Exception($url);
  107. }
  108. // 임시폴더명/저장파일명 얻는다
  109. $tempFile = tempnam(sys_get_temp_dir(), 'php');
  110. // 파일의 originalName을 구한다.
  111. $originalName = explode(sys_get_temp_dir() . '/', $tempFile)[1];
  112. // 파일을 임시폴더에 쓴다.
  113. file_put_contents($tempFile, $stream);
  114. // 프론트에서 업로드 할 때와 같은 파일 Type으로 반환
  115. return new \Illuminate\Http\UploadedFile($tempFile, $originalName, $mimeType, $error, $test);
  116. }
  117. public static function pathToUploadedFile($path, $test = true): \Illuminate\Http\File
  118. {
  119. // $filesystem = new Filesystem;
  120. // $name = $filesystem->name( $path );
  121. // $extension = $filesystem->extension( $path );
  122. // $originalName = $name . '.' . $extension;
  123. // $mimeType = $filesystem->mimeType( $path );
  124. // $error = null;
  125. return new \Illuminate\Http\File( $path );
  126. }
  127. public static function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
  128. {
  129. $exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
  130. array_pop($exploded);
  131. $directoryPathOnly = implode(DIRECTORY_SEPARATOR, $exploded);
  132. if (!\Illuminate\Support\Facades\File::exists($directoryPathOnly))
  133. {
  134. \Illuminate\Support\Facades\File::makeDirectory($directoryPathOnly,0775,true,false);
  135. }
  136. return \Illuminate\Support\Facades\File::put($fullPathWithFileName,$fileContents);
  137. }
  138. }