Skip to content

Commit

Permalink
[File] adds basic info when file has no player
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Feb 9, 2022
1 parent 51eda25 commit a27ca86
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/core/API/Serializer/Resource/Types/FileSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Claroline\CoreBundle\Entity\Resource\ResourceNode;
use Claroline\CoreBundle\Event\GenericDataEvent;
use Claroline\CoreBundle\Event\Resource\File\LoadFileEvent;
use Claroline\CoreBundle\Library\Normalizer\TextNormalizer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

Expand Down Expand Up @@ -48,11 +50,23 @@ public function getName()
*/
public function serialize(File $file): array
{
$options = [
$ext = pathinfo($file->getHashName(), PATHINFO_EXTENSION);
if (empty($ext)) {
$mimeTypeGuesser = new MimeTypes();
$guessedExtension = $mimeTypeGuesser->getExtensions($file->getMimeType());
if (!empty($guessedExtension)) {
$ext = $guessedExtension[0];
}
}

$fileName = TextNormalizer::toKey(str_replace('.'.$ext, '', $file->getResourceNode()->getName())).'.'.$ext;

$serialized = [
'id' => $file->getUuid(),
'size' => $file->getSize(),
'opening' => $file->getOpening(),
'commentsActivated' => $file->getResourceNode()->isCommentsActivated(),
'name' => $fileName, // the name of the file which will be used for file download
'hashName' => $file->getHashName(),

// We generate URL here because the stream API endpoint uses ResourceNode ID,
Expand All @@ -75,7 +89,7 @@ public function serialize(File $file): array
$additionalFileData = $fallBackEvent->getData();
}

return array_merge($additionalFileData, $options);
return array_merge($additionalFileData, $serialized);
}

public function deserialize($data, File $file, array $options = []): File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {File as FileTypes} from '#/main/core/files/prop-types'

import {constants} from '#/main/core/resources/file/constants'
import {ContentComments} from '#/main/app/content/components/comments'
import {PlayerOverview} from '#/main/core/resources/file/player/components/overview'

// TODO : display a standard player with file info if no custom one
const PlayerMain = (props) => {
Expand Down Expand Up @@ -55,6 +56,17 @@ const PlayerMain = (props) => {
}

props.download(props.resourceNode)

console.log(props.mimeType)

return (
<PlayerOverview
file={props.file}
resourceNode={props.resourceNode}
workspace={props.workspace}
download={props.download}
/>
)
}}
/>
)
Expand All @@ -70,6 +82,7 @@ PlayerMain.propTypes = {
file: T.shape(
FileTypes.propTypes
).isRequired,
workspace: T.object,
createComment: T.func.isRequired,
editComment: T.func.isRequired,
deleteComment: T.func.isRequired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, {Fragment} from 'react'
import {PropTypes as T} from 'prop-types'
import get from 'lodash/get'

import {trans, fileSize} from '#/main/app/intl'
import {CALLBACK_BUTTON, URL_BUTTON} from '#/main/app/buttons'
import {Toolbar} from '#/main/app/action'
import {ContentHtml} from '#/main/app/content/components/html'
import {route} from '#/main/core/workspace/routing'

const PlayerOverview = (props) =>
<Fragment>
{get(props.resourceNode, 'meta.description') &&
<div className="panel panel-default" style={{marginTop: 20}}>
<ContentHtml className="panel-body">{get(props.resourceNode, 'meta.description')}</ContentHtml>
</div>
}

<div className="well well-sm component-container" style={{marginTop: !get(props.resourceNode, 'meta.description') ? 20 : 0}}>
<span className="fa fa-fw fa-file icon-with-text-right" />
{props.file.name}
<b className="pull-right">
{fileSize(props.file.size)+trans('bytes_short')}
</b>
</div>

<Toolbar
className="component-container"
buttonName="btn btn-block btn-emphasis"
toolbar="download home"
actions={[
{
name: 'download',
type: CALLBACK_BUTTON,
icon: 'fa fa-fw fa-download',
label: trans('download', {}, 'actions'),
callback: () => props.download(props.resourceNode),
primary: true
}, {
name: 'home',
type: URL_BUTTON, // we require an URL_BUTTON here to escape the embedded resource router
icon: 'fa fa-fw fa-home',
label: trans('return-home', {}, 'actions'),
target: '#'+route(props.workspace),
displayed: !!props.workspace
}
]}
/>
</Fragment>

PlayerOverview.propTypes = {
file: T.shape({
name: T.string.isRequired,
size: T.number
}).isRequired,
resourceNode: T.shape({
name: T.string.isRequired,
meta: T.shape({
description: T.string
})
}),
workspace: T.object,
download: T.func.isRequired
}

export {
PlayerOverview
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const PlayerMain = connect(
mimeType: selectors.mimeType(state),
file: selectors.file(state),
resourceNode: resourceSelectors.resourceNode(state),
workspace: resourceSelectors.workspace(state),
canEdit: hasPermission('edit', resourceSelectors.resourceNode(state))
}),
(dispatch) => ({
Expand Down

0 comments on commit a27ca86

Please sign in to comment.