Advertisement
Guest User

browse.jim

a guest
Feb 26th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.95 KB | None | 0 0
  1. #!/opt/bin/jimsh
  2.  
  3. package require cgi
  4. source /opt/webif/lib/setup
  5. require ts.class pretty_size progressbar
  6.  
  7. puts "Cache-Control: no-cache"
  8. puts "Content-Type: text/html"
  9. puts ""
  10.  
  11. set ignore {.nts .thm .hmt .hmi}
  12. set include {.ts .avi .mpg .mpeg .wmv .mkv .mp3 .mp4 .mov .jpg}
  13. set playable {.ts .mp3}
  14.  
  15. set ::NeedJavaRefresh 0
  16.  
  17. cgi_input
  18. #cgi_dump
  19.  
  20. #set _cgi(dir) "/media/Video"
  21.  
  22. proc directory {file bfile} {
  23. regsub -all " +" $bfile "" tbfile
  24.  
  25. if {$file eq "/media/opt"} {continue}
  26. if {$file eq "/media/Channels"} {continue}
  27.  
  28. puts "<div class=\"va\">"
  29. puts "<img class=\"va\" id=\"img$tbfile\"
  30. src=\"/images/711_1_09_Media_Folder.png\" alt=\"\">"
  31. puts "<a class=\"dbf\"
  32. href=\"/cgi-bin/browse.jim?dir=[cgi_quote_url $file]\"
  33. file=\"[cgi_quote_url $file]\">"
  34. puts "$bfile</a><span class=\"filesize\" id=\"$tbfile\"></span>"
  35.  
  36. # Enable folder delete and rename options on media folders and USB (including NASmounts) only
  37. if {([string first "/media/Video/" $file] != 0) &&
  38. ([string first "/media/Music/" $file] != 0) &&
  39. ([string first "/media/Photo/" $file] != 0) &&
  40. ([string first "/media/sda1/" $file] != 0) &&
  41. ([string first "/media/sdb1/" $file] != 0) &&
  42. ([string first "/media/sdc1/" $file] != 0)} {continue}
  43.  
  44. puts "<a href=\"#\">
  45. <img class=\"dopt va\" width=\"45\" src=\"/images/181_1_00_Help5_OPT_Plus.png\" alt=\"\"></a>
  46. <div class=\"results blood\" style=\"margin: 0 0 0 5em\"></div>
  47.  
  48. </div>"
  49. }
  50.  
  51. proc entry {file} {{i 0}} {
  52. set bfile [file tail $file]
  53. if {[string index $bfile 0] == "\025"} {
  54. set bfile [string range $bfile 1 end]
  55. }
  56. if [file isdirectory "$file"] {
  57. directory $file $bfile
  58. continue
  59. }
  60. set ext [string tolower [file extension $file]]
  61. if {$ext in $::ignore || $ext ni $::include} { continue }
  62.  
  63. file stat $file st
  64. set sz [pretty_size $st(size)]
  65.  
  66. set base [file rootname $file]
  67.  
  68. # Don't show the cache file (0.ts)
  69. if {$bfile eq "0.ts"} { continue }
  70.  
  71. if {$ext eq ".ts" && [file exists "${base}.nts"]} {
  72. set type ts
  73. set ts [ts fetch $file 1]
  74. set img Video_TS
  75. } else {
  76. if {$ext eq ".mp3"} {
  77. set type mp3
  78. set ts 0
  79. set img Video_Other
  80. } else {
  81. if {$ext eq ".jpg"} {
  82. set type jpg
  83. set img Camera
  84. set ts 0
  85. } else {
  86. set type gen
  87. set ts 0
  88. set img Video_Other
  89. }
  90.  
  91. }
  92. }
  93.  
  94. # Determine whether the play button should be displayed
  95. # Only non encrypted ts files and mp3 files
  96. set player 0
  97. if {[file exists /opt/webif/plugin/vlcplayer/player.jim] && ($ext in $::playable)} {
  98. if {$ext eq ".ts" && [file exists "${base}.nts"] && [file exists "${base}.hmt"]} {
  99. if {[$ts flag "Encrypted"] == 0} {
  100. set player 1
  101. }
  102. } else {
  103. set player 1
  104. }
  105. }
  106.  
  107.  
  108. puts "
  109. <div class=\"va bf\" id=\"[incr i]\">
  110. <a class=\"bf\" file=\"$file\" player=\"$player\" type=\"$type\" href=\"#\">
  111. "
  112.  
  113. set new 0
  114. if {$type eq "ts"} {
  115. if {[$ts flag "New"] > 0} { set new 1 }
  116. } else {
  117. if {![file exists "[file rootname $file].hmi"]} { set new 1 }
  118. }
  119.  
  120. if {$new} { append img _New }
  121.  
  122. puts "<img class=\"va\" src=\"/img/$img.png\" alt=\"\">"
  123.  
  124. puts "
  125. $bfile
  126. </a>
  127. "
  128.  
  129. # Size
  130. puts "<span class=\"filesize\"> ($sz) </span>"
  131.  
  132. # Icons
  133.  
  134. set locked 0
  135. set encd 0
  136. set prot 0
  137. set converted 0
  138.  
  139. set def unknown
  140. if {$type eq "ts"} {
  141. # HD / SD
  142. if {[$ts get definition] eq "HD"} {
  143. set def HD
  144. set img "172_1_00_HD"
  145. } else {
  146. set def SD
  147. set img "172_1_26_SD"
  148. }
  149. puts "<img class=\"va\" src=\"/images/$img.png\" height=\"21\" alt=\"\">"
  150.  
  151. # Locked
  152. if {[$ts flag "Locked"] > 0} {
  153. set locked 1
  154. puts "<img class=\"va\" src=\"/images/178_1_00_Icon_Lock.png\"
  155. height=\"21\" alt=\"\">"
  156. }
  157.  
  158. # Protected
  159. if {[$ts flag "Protected"] > 0} {
  160. set prot 1
  161. puts "<img class=\"va\" src=\"/images/norecycle_32.png\"
  162. height=\"21\" alt=\"\">"
  163. }
  164.  
  165. # Encrypted
  166. if {[$ts flag "Encrypted"] > 0} {
  167. set encd 1
  168. puts "<img class=\"va\"
  169. src=\"/images/749_1_26_Video_Encryption.png\"
  170. height=\"21\" alt=\"\">"
  171. # Disable the OPT+ 'convert' option
  172. set converted 0
  173. }
  174.  
  175. # Guidance
  176. if {[$ts flag "Guidance"] > 0} {
  177. puts "<img class=\"va\"
  178. src=\"/images/174_1_26_GuidancePolicy.png\"
  179. height=\"21\" alt=\"\">"
  180. }
  181.  
  182. # New Flag
  183. if {[$ts flag "New"] > 0} {
  184. puts "<img class=\"va\"
  185. src=\"/images/New.png\"
  186. height=\"21\" alt=\"\">"
  187. }
  188.  
  189. # File Conversion Flags
  190. # Don't show anything if file is unconverted
  191. # Show small progress bar if in progress or fully converted
  192. if {[$ts flag "Converting"] > 0} {
  193. # Default state is to assume that the file IS actually being converted so
  194. # disable the OPT+ option to convert
  195. set converted 1
  196.  
  197. # But check becuase this could be a file conversion that terminated e.g. because of a HDR reboot
  198. # See if there's a lock file and if there is, whether it refers to this file
  199. # If there's no lock file or the lock file referes to a different file then this conversion
  200. # has terminated half way through so allow conversion
  201. set ReallyConverting 1
  202. set curFile ""
  203.  
  204. if {![file exists "/tmp/convert-ts.lock"]} {
  205. set ReallyConverting 0
  206. } else {
  207. if {![catch {set lock_fd [open "/tmp/convert-ts.lock" r]}]} {
  208. set curFile [read $lock_fd]
  209. set curFile [string map {"/mnt/hd3/Video/" "/media/Video/"} $curFile]
  210. $lock_fd close
  211. }
  212. if {$curFile ne $file} {
  213. set ReallyConverting 0
  214. }
  215. }
  216.  
  217. if {$ReallyConverting == 1} {
  218. set tmpfile ${base}.tmp
  219. if {[file exists $tmpfile]} {
  220. set perc [format %.1f [expr {100.0*[file size $file]/[file size $tmpfile]}]]
  221. set imgfile [format "%02.0f" [expr {$perc * 25 / 100 + 1}]]
  222. # Account for possible split second where file is fully converted but the HMT flag
  223. # has not yet been updated - so % complete = 100% and imgfile would be 26!
  224. if {$imgfile == "26"} {
  225. set imgfile "25"
  226. }
  227.  
  228. set prefix 345_2_14_ST_HDD
  229. set ::NeedJavaRefresh 1
  230.  
  231. puts " <input type=\"hidden\" id=\"RefreshFileName\" name=\"RefreshFileName\" value=\"$file\">
  232. <img id=\"RefreshImage\" style=\"background:url('/images/345_1_27_ST_USB_BG.png') no-repeat; background-size: 21px 21px\"
  233. src=\"/images/${prefix}_$imgfile.png\" alt=\"$perc%\" height=\"21\">
  234. <span id=\"RefreshSpan\" style=\"font-size: 80%; font-weight: bold;\">$perc%</span>
  235. "
  236. }
  237. } else {
  238. set converted 0
  239. }
  240. }
  241.  
  242. if {[$ts flag "Converted"] > 0} {
  243. # Disable the OPT+ 'convert' option
  244. set converted 1
  245. puts "<img class=\"va\"
  246. src=\"/images/dlna.png\"
  247. height=\"21\" alt=\"\">"
  248. }
  249.  
  250. }
  251.  
  252. # Opt+ button
  253.  
  254. puts "
  255. <a href=\"#\">
  256. <img class=\"opt va\" width=\"45\" src=\"/images/181_1_00_Help5_OPT_Plus.png\" alt=\"\"
  257. type=\"$type\" did=\"$i\" locked=\"$locked\" encd=\"$encd\" def=\"$def\" new=\"$new\" protected=\"$prot\" converted=\"$converted\">
  258. </a>
  259. <div class=\"results blood\" style=\"margin: 0 0 0 5em\"></div>
  260. "
  261. puts "</div>"
  262. }
  263.  
  264. if {[dict exists $_cgi dir]} {
  265. set dir [dict get $_cgi dir]
  266. } else {
  267. set dir "/media/Video"
  268. }
  269.  
  270. ######################################################################
  271. # Render web page
  272.  
  273. header
  274.  
  275. puts {
  276. <script type="text/javascript" src="/js/jquery.contextMenu.js"></script>
  277. <link href=/css/jquery.contextMenu.css rel=stylesheet type=text/css />
  278.  
  279. <ul id=optmenu class=contextMenu>
  280. <li class="delete"><a href=#delete>Delete</a></li>
  281. <li class="lock"><a href=#lock>Toggle Lock</a></li>
  282. <li class="protect"><a href=#prot>Toggle Protected</a></li>
  283. <li class="new"><a href=#new>Toggle New</a></li>
  284. <li class="rename"><a href=#rename>Rename</a></li>
  285. <li class="download"><a href=#download>Download</a></li>
  286. }
  287. if {[file exists "/opt/bin/convert-ts"]} {
  288. puts {<li class="convert"><a href=#convert>Convert for Streaming</a></li>}
  289. }
  290. puts {</ul>}
  291.  
  292. puts {
  293. <ul id=doptmenu class=contextMenu>
  294. <li class="delete"><a href=#delete>Delete</a></li>
  295. <li class="folder"><a href=#rename>Rename</a></li>
  296. </ul>
  297. }
  298.  
  299. puts {
  300. <div id=renamefile title="Rename Media File" style="display: none">
  301. <form id="renamefile_form">
  302. <input type="hidden" name="origfname" id="origfname" value="">
  303. <input type="hidden" name="origtitle" id="origtitle" value="">
  304. <table style="border:0">
  305. <tr>
  306. <th>
  307. <label for="newfname">
  308. <b>New File Name</b>
  309. </label>
  310. </th>
  311. <td>
  312. <input type=text name="newfname" id="newfname"
  313. value="" size="70" maxlength="255"
  314. class="text ui-widget-content ui-corner-all">
  315. </td>
  316. </tr>
  317. <tr style="display: none" class=tstype>
  318. <th>
  319. <label for="newtitle" style="padding-top: 0.5em">
  320. <b>New EPG Title</b>
  321. </label>
  322. </th>
  323. <td>
  324. <input type=text name="newtitle" id="newtitle"
  325. value="" size="70" maxlength="250"
  326. class="text ui-widget-content ui-corner-all">
  327. </td>
  328. </tr>
  329. <tr style="display: none" class="tstype">
  330. <td colspan="2" id="synopsis" style="font-style: italic"></td>
  331. </tr>
  332. </table>
  333. </form>
  334. </div>
  335.  
  336. <div id=renamedir title="Rename Media Folder" style="display: none">
  337. <form id="renamedir_form">
  338. <input type="hidden" name="origdname" id="origdname" value="">
  339. <table style="border:0">
  340. <tr>
  341. <th>
  342. <label for="newdname">
  343. <b>New Folder Name</b>
  344. </label>
  345. </th>
  346. <td>
  347. <input type=text name="newdname" id="newdname"
  348. value="" size="70" maxlength="255"
  349. class="text ui-widget-content ui-corner-all">
  350. </td>
  351. </tr>
  352. </table>
  353. <div id="renamedirout"><span class="footnote"></span></div>
  354. </form>
  355. </div>
  356.  
  357. <div id="dialogue"></div>
  358. <div id="confirm" title="Confirmation Required"></div>
  359.  
  360. <script type="text/javascript" src="/cgi-bin/browse/browse.js"></script>
  361. }
  362. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  363. puts "<span style=\"display:none\" id=dir>$dir</span>"
  364.  
  365. # Breadcrumb path
  366. puts "
  367. <fieldset style=\"margin: 1em\">
  368. <legend style=\"font-size: 1.5em; padding: 0 0.5em 0.5em 0.5em;\">
  369. "
  370. set stub ""
  371.  
  372. foreach part [split $dir /] {
  373. if {$stub eq "/"} { set name $part } else { set name "/$part" }
  374. append stub $name
  375. puts "<a href=\"/cgi-bin/browse.jim?dir=[cgi_quote_url $stub]\">$name</a>
  376. }
  377. puts "<span class=\"filesize\" id=\"dirsize\"></span>"
  378. puts "</legend>"
  379.  
  380. # Parent directory
  381. set parent [join [lrange [split $dir /] 0 end-1] /]
  382. if {$parent ne ""} {
  383. puts "
  384. <div class=\"va\">
  385. <a href=\"/cgi-bin/browse.jim?dir=[cgi_quote_url $parent]\">
  386. <img src=\"/images/711_3_09_Media_Folder_UP.png\" class=\"va\" alt=\"\">
  387. \[parent directory\]</a>
  388. </div>
  389. "
  390. }
  391.  
  392. # Strip double slashes
  393. regsub -all -- {\/+} "$dir/*" "/" dir
  394.  
  395. # Account for spaces in the path name
  396. regsub -all -- { +} "$dir" "\\ " dir
  397.  
  398. foreach file [lsort [glob -nocomplain "$dir"]] {
  399. # Check for unicode characters in filename
  400. # Ignore if present
  401. if {[string is ascii -strict $file] == 1} {
  402. entry $file
  403. }
  404. }
  405.  
  406. puts "</fieldset>"
  407.  
  408. if {$::NeedJavaRefresh == 1} {
  409. puts {
  410. <script langauge="javascript">
  411. window.setInterval("refreshDiv()", 2500);
  412. function refreshDiv(){
  413. var fname = document.getElementById("RefreshFileName").value;
  414.  
  415. var http = new XMLHttpRequest();
  416. var url = '/cgi-bin/browse/convertprogress.jim?file=' + encodeURIComponent(fname);
  417.  
  418. http.open('GET', url, false);
  419. http.send();
  420.  
  421. var resptext = http.responseText.trim();
  422. var respsplit = resptext.split("|");
  423. var perc = respsplit[0];
  424. var imgsrc = respsplit[1];
  425.  
  426. if (imgsrc == '/images/345_2_14_ST_HDD_26.png') {
  427. // No more refreshing - file is converted - reload screen
  428. document.location.reload();
  429. } else {
  430. $("#RefreshImage").attr('src', imgsrc);
  431. $("#RefreshSpan").html(perc + '%');
  432. }
  433. }
  434. </script>
  435. }
  436. }
  437.  
  438. footer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement