MusicController.php 3.7 KB

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