@@ -4,6 +4,10 @@ import {AppBar, Box, Divider, Grid, Tab, Tabs, Typography} from "@material-ui/co
44import { makeStyles } from "@material-ui/core/styles" ;
55import { ClowderInput } from "./styledComponents/ClowderInput" ;
66import { ClowderButton } from "./styledComponents/ClowderButton" ;
7+ import Audio from "./previewers/Audio" ;
8+ import Video from "./previewers/Video" ;
9+ import { downloadResource } from "../utils/common" ;
10+ import Thumbnail from "./previewers/Thumbnail" ;
711
812const useStyles = makeStyles ( ( theme ) => ( {
913 appBar : {
@@ -25,47 +29,42 @@ export default function File(props) {
2529 const { fileMetadata, fileExtractedMetadata, fileMetadataJsonld, filePreviews, fileId, ...other } = props ;
2630
2731 const [ selectedTabIndex , setSelectedTabIndex ] = useState ( 0 ) ;
32+ const [ previews , setPreviews ] = useState ( [ ] ) ;
2833
29- // component did mount
30- useEffect ( ( ) => {
31- // attach helper jquery
32- const script = document . createElement ( "script" ) ;
33- script . src = `../public/clowder/assets/javascripts/previewers/helper.js` ;
34- script . async = true ;
35- document . body . appendChild ( script ) ;
36- return ( ) => {
37- document . body . removeChild ( script ) ;
38- }
39- } , [ ] ) ;
34+ // // component did mount
35+ // useEffect(() => {
36+ // // attach helper jquery
37+ // const script = document.createElement("script");
38+ // script.src = `../public/clowder/assets/javascripts/previewers/helper.js`;
39+ // script.async = true;
40+ // document.body.appendChild(script);
41+ // return () => {
42+ // document.body.removeChild(script);
43+ // }
44+ // }, []);
4045
4146 useEffect ( ( ) => {
42- // remove last previewer script attached
43- const previewerScripts = document . getElementsByClassName ( "previewer-script" ) ;
44- while ( previewerScripts . length > 0 ) {
45- previewerScripts [ 0 ] . parentNode . removeChild ( previewerScripts [ 0 ] ) ;
46- }
47+ ( async ( ) => {
48+ if ( filePreviews !== undefined && filePreviews . length > 0 && filePreviews [ 0 ] . previews !== undefined ) {
49+ let previewsTemp = [ ] ;
50+ await Promise . all ( filePreviews [ 0 ] . previews . map ( async ( filePreview ) => {
51+ // download resources
52+ let Configuration = { } ;
53+ Configuration . previewType = filePreview [ "p_id" ] . replace ( " " , "-" ) . toLowerCase ( ) ;
54+ Configuration . url = `${ config . hostname } ${ filePreview [ "pv_route" ] } ?superAdmin=true` ;
55+ Configuration . fileid = filePreview [ "pv_id" ] ;
56+ Configuration . previewer = `/public${ filePreview [ "p_path" ] } /` ;
57+ Configuration . fileType = filePreview [ "pv_contenttype" ] ;
4758
48- if ( filePreviews !== undefined && filePreviews . length > 0 && filePreviews [ 0 ] . previews !== undefined ) {
49- let uniquePid = [ ] ;
50- // look at which previewer to load
51- filePreviews [ 0 ] . previews . map ( ( filePreview , index ) => {
59+ let resourceURL = `${ config . hostname } ${ filePreview [ "pv_route" ] } ?superAdmin=true` ;
60+ Configuration . resource = await downloadResource ( resourceURL ) ;
5261
53- // do not attach same previewer twice
54- if ( uniquePid . indexOf ( filePreview [ "p_id" ] ) === - 1 ) {
55- uniquePid . push ( filePreview [ "p_id" ] ) ;
62+ previewsTemp . push ( Configuration ) ;
5663
57- // attach previwer jquery
58- const script = document . createElement ( "script" ) ;
59- script . className = "previewer-script" ;
60- script . src = `../public${ filePreview [ "p_path" ] } /${ filePreview [ "p_main" ] } ` ;
61- script . async = true ;
62- document . body . appendChild ( script ) ;
63- return ( ) => {
64- document . body . removeChild ( script ) ;
65- }
66- }
67- } ) ;
68- }
64+ } ) ) ;
65+ setPreviews ( previewsTemp ) ;
66+ }
67+ } ) ( ) ;
6968 } , [ filePreviews ] ) ;
7069
7170 const handleTabChange = ( event , newTabIndex ) => {
@@ -87,27 +86,18 @@ export default function File(props) {
8786 </ AppBar >
8887 < TabPanel value = { selectedTabIndex } index = { 0 } >
8988 {
90- filePreviews !== undefined && filePreviews . length > 0 && filePreviews [ 0 ] . previews !== undefined
91- ?
92- filePreviews [ 0 ] . previews . map ( ( filePreview , index ) => {
93- const Configuration = { } ;
94- Configuration . tab = `#previewer_${ filePreviews [ 0 ] [ "file_id" ] } _${ index } ` ;
95- Configuration . url = `${ config . hostname } ${ filePreview [ "pv_route" ] } ?superAdmin=true` ;
96- Configuration . fileid = filePreview [ "pv_id" ] ;
97- Configuration . previewer = `/public${ filePreview [ "p_path" ] } /` ;
98- Configuration . fileType = filePreview [ "pv_contenttype" ] ;
99- Configuration . APIKEY = config . apikey ;
100- Configuration . authenticated = true ;
101- // Configuration.metadataJsonld = fileMetadataJsonld;
102-
103- let previewId = filePreview [ "p_id" ] . replace ( " " , "-" ) . toLowerCase ( ) ;
104- return ( < div className = { `configuration ${ previewId } ` }
105- data-configuration = { JSON . stringify ( Configuration ) } >
106- < div id = { Configuration . tab . slice ( 1 ) } > </ div >
107- </ div > ) ;
108- } )
109- :
110- < > </ >
89+ previews . map ( ( preview ) => {
90+ if ( preview [ "previewType" ] === "audio" ) {
91+ return < Audio fileId = { preview [ "fileid" ] } audioSrc = { preview [ "resource" ] } /> ;
92+ }
93+ else if ( preview [ "previewType" ] === "video" ) {
94+ return < Video fileId = { preview [ "fileid" ] } videoSrc = { preview [ "resource" ] } /> ;
95+ }
96+ else if ( preview [ "previewType" ] === "thumbnail" ) {
97+ return < Thumbnail fileId = { preview [ "fileid" ] } fileType = { preview [ "fileType" ] }
98+ imgSrc = { preview [ "resource" ] } /> ;
99+ }
100+ } )
111101 }
112102 </ TabPanel >
113103 < TabPanel value = { selectedTabIndex } index = { 1 } >
0 commit comments