MusicController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Music;
  4. use App\Settings;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Session;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. class MusicController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. // list all songs
  19. $songs = Music::all();
  20. $settings = dataManipulator::objArrToAssoc(Settings::all());
  21. return view('home', ['song_list' => $songs, 'srv_state'=>$settings]);
  22. }
  23. public function playLog(Music $music)
  24. {
  25. // add one to played log
  26. $music->played += 1;
  27. $music->save();
  28. return $music->played;
  29. }
  30. public function dwLog(Music $music)
  31. {
  32. // add one to download log
  33. $music->downloads += 1;
  34. $music->save();
  35. return $music->downloads;
  36. }
  37. public function fileRefresh()
  38. {
  39. // Scan for files
  40. $fIndex = array();
  41. try {
  42. $files = Storage::disk('local')->listContents('public/audio', true);
  43. } catch (\Exception $e){
  44. $files = array();
  45. }
  46. //return $files;
  47. // Update storage index
  48. $songs = Music::all();
  49. foreach ($files as $f) {
  50. $fix_path = Str::replaceFirst('public/', 'storage/', $f['path']);
  51. $melody_db = Music::where('file_name', $fix_path)->first();
  52. // add NEW files to DB
  53. if (!$melody_db) {
  54. $melody = new Music();
  55. $melody->title = $f['filename'];
  56. $melody->description = $f['filename'];
  57. $melody->played = 0;
  58. $melody->downloads = 0;
  59. $melody->hidden = 0; // visible
  60. $melody->file_name = $fix_path;
  61. if ($f['type'] == 'dir') {
  62. $melody->type = 'dir';
  63. } elseif ($f['type'] == 'file') {
  64. if ($f['extension'] == 'txt') {
  65. $melody->type = 'txt';
  66. $melody->description = Storage::disk('local')->read($f['path']);
  67. } else {
  68. $melody->type = $f['extension'];
  69. }
  70. }
  71. $melody->save();
  72. } else {
  73. // Update description, even if it's already fetched to db
  74. // Update data ONLY from TXT files, leave folders and mp3 as it is
  75. if ($f['type'] == 'file' && $f['extension'] == 'txt') {
  76. $melody = $melody_db;
  77. $melody->description = Storage::disk('local')->read($f['path']);
  78. $melody->save();
  79. }
  80. }
  81. $fIndex[] = $fix_path;
  82. }
  83. // Clean DB of OLD files
  84. $musicdB = Music::all();
  85. foreach ($musicdB as $song) {
  86. if (!in_array($song->file_name, $fIndex)) {
  87. $song->delete();
  88. }
  89. }
  90. return $files;
  91. }
  92. /**
  93. * Update the specified resource in storage.
  94. *
  95. * @param \Illuminate\Http\Request $request
  96. * @param \App\Music $music
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function update(Request $request, Music $music)
  100. {
  101. $music->title = $request->newname;
  102. $music->save();
  103. return true;
  104. }
  105. /**
  106. * Show the form for creating a new resource.
  107. *
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function create()
  111. {
  112. //
  113. }
  114. /**
  115. * Store a newly created resource in storage.
  116. *
  117. * @param \Illuminate\Http\Request $request
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function store(Request $request)
  121. {
  122. //
  123. }
  124. /**
  125. * Display the specified resource.
  126. *
  127. * @param \App\Music $music
  128. * @return \Illuminate\Http\Response
  129. */
  130. public function show(Music $music)
  131. {
  132. //
  133. }
  134. /**
  135. * Show the form for editing the specified resource.
  136. *
  137. * @param \App\Music $music
  138. * @return \Illuminate\Http\Response
  139. */
  140. public function edit(Music $music)
  141. {
  142. //
  143. }
  144. /**
  145. * Remove the specified resource from storage.
  146. *
  147. * @param \App\Music $music
  148. * @return \Illuminate\Http\Response
  149. */
  150. public function destroy(Music $music)
  151. {
  152. //
  153. }
  154. }