Skip to content

A self-hosted media server and file management system

add view count

aminnausinc9df52cc22a8
diff --git a/app/Http/Controllers/Api/V1/VideoController.php b/app/Http/Controllers/Api/V1/VideoController.php
index 09e48633..eef25b65 100644
--- a/app/Http/Controllers/Api/V1/VideoController.php
+++ b/app/Http/Controllers/Api/V1/VideoController.php
@@ -41,4 +41,30 @@ class VideoController extends Controller
{
return new VideoResource($video);
}
+
+ /**
+ * Update the specified resource in storage.
+ *
+ * @param int $id
+ * @return \Illuminate\Http\Response
+ */
+ public function update(Request $request, Video $video)
+ {
+ $video->update($request->all());
+
+ return $this->success(new VideoResource($video));
+ }
+
+ /**
+ * Update view counter
+ *
+ * @param int $id
+ * @return \Illuminate\Http\Response
+ */
+ public function watch(Request $request, Video $video)
+ {
+ $video->update(['view_count' => ($video->view_count ?? 0) + 1]);
+
+ return $this->success(new VideoResource($video));
+ }
}
diff --git a/app/Http/Requests/VideoUpdateRequest.php b/app/Http/Requests/VideoUpdateRequest.php
new file mode 100644
index 00000000..0ffcf909
--- /dev/null
+++ b/app/Http/Requests/VideoUpdateRequest.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class VideoCollectionRequest extends FormRequest
+{
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool
+ {
+ return true;
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array
+ {
+ return [
+ 'folder_id' => ['required']
+ ];
+ }
+}
diff --git a/resources/js/components/user/UserDropdown.vue b/resources/js/components/user/UserDropdown.vue
index 793bc7b0..8cac4faa 100644
--- a/resources/js/components/user/UserDropdown.vue
+++ b/resources/js/components/user/UserDropdown.vue
@@ -20,6 +20,7 @@
{name:'history', url:'/history', text:'Full History'},
{name:'index', url:'/jobs/indexFiles', text:'Index Files', external: true},
{name:'sync', url:'/jobs/syncFiles', text:'Sync Files', external: true},
+ {name:'sync', url:'/jobs/verifyFiles', text:'Verify Files', external: true},
];
</script>
diff --git a/resources/js/components/video/VideoPlayer.vue b/resources/js/components/video/VideoPlayer.vue
index dbc22504..57623b8a 100644
--- a/resources/js/components/video/VideoPlayer.vue
+++ b/resources/js/components/video/VideoPlayer.vue
@@ -82,26 +82,29 @@ const heatMap = computed(() => {
return start + catmullRomFitting(heatMapData.value, 0.5);
});
-const pastFirst = ref(false);
const currentID = ref(-1);
const ContentStore = useContentStore();
const { stateVideo } = storeToRefs(ContentStore);
-const { createRecord } = ContentStore;
+const { createRecord, updateViewCount } = ContentStore;
const initVideoPlayer = () => {
- let vidSource = document.getElementById('vid-source');
+ // let vidSource = document.getElementById('vid-source');
let root = document.getElementById('root');
root.scrollIntoView();
- if (pastFirst.value === true) vidSource.play();
+ // if (pastFirst.value === true) vidSource.play();
}
const playVideo = () => {
- if(currentID.value === stateVideo.value.id) return; // stop recording every time video seek
- pastFirst.value = true;
+ console.log(stateVideo.value.id);
+ if(currentID.value === stateVideo.value.id){
+ console.log(stateVideo.value.id + ' already seen');
+ return;
+ }// stop recording every time video seek
currentID.value = stateVideo.value.id;
createRecord(stateVideo.value.id);
+ updateViewCount(stateVideo.value.id);
}
watch(stateVideo, initVideoPlayer)
@@ -110,7 +113,7 @@ watch(stateVideo, initVideoPlayer)
<template>
<div class="relative group">
<video id="vid-source" width="100%" :src="stateVideo?.attributes?.path ? `../${stateVideo?.attributes?.path}` : ''" type="video/mp4" controls
- class="focus:outline-none aspect-video flex" @play="playVideo" :autoplay="pastFirst === true">
+ class="focus:outline-none aspect-video flex" @play="playVideo">
<track kind="captions">
</video>
<section class="absolute bottom-6 w-full hidden px-[3%]"> <!-- group-hover:block -->
diff --git a/resources/js/service/mediaAPI.js b/resources/js/service/mediaAPI.js
index 249386ca..40587d90 100644
--- a/resources/js/service/mediaAPI.js
+++ b/resources/js/service/mediaAPI.js
@@ -10,6 +10,9 @@ export default{
getVideos(data) {
return API.post('/videos', data);
},
+ viewVideo(id) {
+ return API.patch(`/videos/watch/${id}`)
+ },
getCategory(query) {
return API.get(`/${query}`);
}
diff --git a/resources/js/service/util.js b/resources/js/service/util.js
index 8d3c066c..ebc93cec 100644
--- a/resources/js/service/util.js
+++ b/resources/js/service/util.js
@@ -13,7 +13,7 @@ export function toTimeSpan(rawDate) {
const minutes = Math.round(rawAge / (1000 * 60));
const seconds = Math.round(rawAge / (1000));
- const timeSpan = weeks > 0 ? `${weeks} week${weeks > 1 ? 's' : ''} ago` : days > 0 ? `${days} day${days > 1 ? 's' : ''} ago` : hours > 0 ? `${hours} hour${hours > 1 ? 's' : ''} ago` : minutes > 0 ? `${minutes}m ago` : `${seconds}s ago`
+ const timeSpan = weeks > 0 ? `${weeks} week${weeks > 1 ? 's' : ''} ago` : days > 0 ? `${days} day${days > 1 ? 's' : ''} ago` : hours > 0 ? `${hours} hour${hours > 1 ? 's' : ''} ago` : minutes > 0 ? `${minutes}m ago` : `${seconds}s ago`;
return timeSpan;
}
@@ -22,6 +22,17 @@ export function toFormattedDate(rawDate) {
return (rawDate.toLocaleString([], {year: 'numeric', month: '2-digit', day:'2-digit', hour:'2-digit', minute:'2-digit', hour12: true})).toLocaleUpperCase().replaceAll('.','');
}
+export function toFormattedDuration(rawSeconds) {
+ if(isNaN(parseInt(rawSeconds))) return null;
+ const hours = Math.round(rawSeconds / (3600));
+ const minutes = Math.round((rawSeconds % 3600) / (60));
+ const seconds = Math.round(rawSeconds % 60);
+
+ const duration = `${hours > 0 ? `${hours}h `:' '}${minutes > 0 ? `${minutes}m `:' '}${`${formatInteger(seconds)}s`}`;
+
+ return duration;
+}
+
export function formatInteger(integer, minimumDigits = 2) {
return integer.toLocaleString('en-CA', { minimumIntegerDigits: minimumDigits})
}
\ No newline at end of file
diff --git a/resources/js/stores/ContentStore.js b/resources/js/stores/ContentStore.js
index 77692f3b..8ba44bd6 100644
--- a/resources/js/stores/ContentStore.js
+++ b/resources/js/stores/ContentStore.js
@@ -29,6 +29,17 @@ export const useContentStore = defineStore('Content', () => {
const filterQuery = ref({search: '', season: -1, tag: ''});
const sortQuery = ref({name: 0, date: 0});
+ async function updateViewCount(id){
+ const { data, error } = await mediaAPI.viewVideo(id);
+
+ if(error || !data?.success){
+ console.log(error ?? data?.message);
+ return Promise.reject([]);
+ }
+ stateVideo.value.attributes.view_count += 1;
+ return Promise.resolve(stateVideo.value)
+ }
+
async function getRecords(limit){
if(!userData.value) return;
@@ -137,6 +148,8 @@ export const useContentStore = defineStore('Content', () => {
// InitPlaylist (set up playlist with indexes and current video)
async function InitPlaylist(){
+ filterQuery.value = {search: '', season: -1, tag: ''};
+ sortQuery.value = {name: 0, date: 0};
if(!stateFolder.value.id){
// eslint-disable-next-line no-undef
toastr["error"](`The folder '${stateFolder.value.name}' does not exist.`, "Invalid folder");
@@ -144,9 +157,9 @@ export const useContentStore = defineStore('Content', () => {
}
statePlaylist.value = stateFolder.value.videos.map((video, index) => { return {index, ...video}; } );
- stateVideo.value = statePlaylist.value.length > 0 ? statePlaylist.value[0] : {};
playlistSort();
- playlistFilter();
+ stateVideo.value = statePlaylist.value.length > 0 ? statePlaylist.value[0] : {};
+ searchQuery.value = '';
}
const playlistSort = (column = 'name', dir = 1) => {
@@ -190,6 +203,8 @@ export const useContentStore = defineStore('Content', () => {
stateDirectory, stateFolder, stateVideo,
searchQuery, filterQuery, stateFilteredPlaylist,
getRecords, createRecord, deleteRecord,
- getCategory, getFolder, playlistSeek, playlistFind, playlistSort
+ getCategory, getFolder,
+ updateViewCount,
+ playlistSeek, playlistFind, playlistSort
};
});
\ No newline at end of file
diff --git a/routes/api.php b/routes/api.php
index be725160..37f896ef 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -18,10 +18,12 @@ Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
Route::resource('/videos', VideoController::class);
Route::resource('/folders', FolderController::class);
-Route::post('/videos', [VideoController::class, 'getFrom']);
-Route::post('/folders', [FolderController::class, 'getFrom']);
Route::post('/login', [AuthController::class, 'login']); // Deprecate
Route::post('/register', [AuthController::class, 'register']); // Deprecate
+Route::post('/videos', [VideoController::class, 'getFrom']);
+Route::patch('/videos/watch/{video}', [VideoController::class, 'watch']);
+Route::post('/folders', [FolderController::class, 'getFrom']);
+
// protected