I’m developing a Web application (vanilla Javascript) aimed to work fully locally at the client side (i.e. there is nothing being retrieved from any external server; in fact, the whole application can be stored into a memory stick or external storage device and invoked even if there is no internet connection at all). The application is aimed to manage media files (pictures as well as video clips, all stored locally).
As part of its functionality, I’m trying to write a piece of code that, upon user’s request, will scan and check that each and every media file reference contained within an index file does have a matching media file within a repository folder.
Following some hints I found in the internet it would appear that the solution would be to create a img and video elements (including a source element as a child of the video element), try to set their respective source to point to the file within the repository and catch an event when the load attempt fails (i.e. there is no such file in repository).
So far, neither type of media file is properly handled:
- missing mpg files are not detected (message are not shown),
- All the mp4 files are shown as missing.
The loop (that scans through the index file) is using an interval to allow some time for the loading to execute. This interval is set to 1ms since (and this is my assumption), if the searched file is missing, the error will be immediately triggered.
The relevant part of the code handling this scanning process follows.
var l_Tempo_Image_Element = document.createElement("img" ) ;
var l_Tempo_Clip_Element = document.createElement("video") ;
var l_Tempo_Clip_Source = document.createElement("source") ;
function f_Progress_1_Step() {
if ((l_Progress_Bar_Width_seed > l_Albums_Count) ||
(G_Halt_Consistency_Check == true ) ){
clearInterval(l_Interval);
G_Halt_Consistency_Check = false ;
l_Progress_Bar_OBJ.style.width = (l_Progress_Bar_Width_seed / l_Albums_Count) * 100 + "%";
l_Progress_Num_OBJ.textContent = (l_Progress_Bar_Width_seed - 1) + " / " + l_Albums_Count ;
l_Scan_Index = 0;
} else {
l_Scan_Index = l_Scan_Index + 1
l_Update_Steps_Counter = l_Update_Steps_Counter + 1 ;
l_Progress_Bar_Width_seed = l_Progress_Bar_Width_seed + 1 ;
if (l_Update_Steps_Counter == l_Update_Steps) {
l_Progress_Bar_OBJ.style.width = (l_Progress_Bar_Width_seed / l_Albums_Count) * 100 + "%";
l_Progress_Num_OBJ.textContent = l_Progress_Bar_Width_seed + " / " + l_Albums_Count ;
l_Update_Steps_Counter = 0 ;
}
}
try{
if (l_Album_ID != G_Display_List[l_Scan_Index].Album_ID){
l_Album_ID = G_Display_List[l_Scan_Index].Album_ID ;
console.log("Checking Index consistency for Album " + G_All_Albums[G_Display_List[l_Scan_Index].Album_ID].Album_Title) ;
}
if (G_Display_List[l_Scan_Index].Type.toUpperCase() == "JPG") {
l_Tempo_Image_Element.setAttribute("onerror" , "console.log('Missing file Image'") ;
l_Tempo_Image_Element.setAttribute ("src", G_All_Media_Files_Location + G_Display_List[l_Scan_Index].File) ;
}
else if (G_Display_List[i].Type.toUpperCase() == "MP4") {
l_Tempo_Clip_Element.setAttribute("onerror" , "console.log('Missing file Video!')" ) ;
l_Tempo_Clip_Source.setAttribute("onerror" , "console.log('Missing file Video!')" ) ;
l_Tempo_Clip_Element.setAttribute ("src", G_All_Media_Files_Location + G_Display_List[l_Scan_Index].File) ;
console.log("Checking file: " + G_All_Media_Files_Location + G_Display_List[l_Scan_Index].File)
}
}
catch {
console.log("Missing file index is: " + l_Scan_Index + " - File: " + G_Display_List[l_Scan_Index].File)
}
}
Any help will be much appreciated.
Cheers.