2019-01-04 10:30:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import Account from './components/common/account';
|
2019-01-24 07:41:01 +00:00
|
|
|
import { serviceURL, gettext, siteRoot, mediaUrl, logoPath, logoWidth, logoHeight, siteTitle } from './utils/constants';
|
2019-01-04 10:30:03 +00:00
|
|
|
import { Button } from 'reactstrap';
|
|
|
|
import { seafileAPI } from './utils/seafile-api';
|
|
|
|
import { Utils } from './utils/utils';
|
|
|
|
import Loading from './components/loading';
|
|
|
|
import SaveSharedFileDialog from './components/dialog/save-shared-file-dialog';
|
|
|
|
import toaster from './components/toast';
|
|
|
|
import watermark from 'watermark-dom';
|
|
|
|
import MarkdownViewer from '@seafile/seafile-editor/dist/viewer/markdown-viewer';
|
|
|
|
|
|
|
|
import './css/shared-file-view.css';
|
|
|
|
import './assets/css/fa-solid.css';
|
|
|
|
import './assets/css/fa-regular.css';
|
|
|
|
import './assets/css/fontawesome.css';
|
|
|
|
|
|
|
|
let loginUser = window.app.pageOptions.name;
|
2019-01-24 07:41:01 +00:00
|
|
|
const { repoID, sharedToken, trafficOverLimit, fileName, fileSize, rawPath, sharedBy, siteName, enableWatermark, download } = window.shared.pageOptions;
|
2019-01-04 10:30:03 +00:00
|
|
|
|
|
|
|
class SharedFileViewMarkdown extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
markdownContent: '',
|
|
|
|
loading: true,
|
|
|
|
showSaveSharedFileDialog: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSaveSharedFileDialog = () => {
|
|
|
|
this.setState({
|
|
|
|
showSaveSharedFileDialog: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleCancel = () => {
|
|
|
|
this.setState({
|
|
|
|
showSaveSharedFileDialog: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSaveSharedFile = () => {
|
|
|
|
toaster.success(gettext('Successfully saved'), {
|
|
|
|
duration: 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
seafileAPI.getFileContent(rawPath).then((res) => {
|
|
|
|
this.setState({
|
|
|
|
markdownContent: res.data,
|
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
});
|
2019-01-31 09:37:02 +00:00
|
|
|
if (trafficOverLimit == 'True') {
|
2019-01-04 10:30:03 +00:00
|
|
|
toaster.danger(gettext('File download is disabled: the share link traffic of owner is used up.'), {
|
|
|
|
duration: 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 07:41:01 +00:00
|
|
|
changeImageURL = (innerNode) => {
|
|
|
|
if (innerNode.type == 'image') {
|
|
|
|
let imageUrl = innerNode.data.src;
|
|
|
|
|
|
|
|
const re = new RegExp(serviceURL + '/lib/' + repoID +'/file.*raw=1');
|
|
|
|
|
|
|
|
// different repo
|
|
|
|
if (!re.test(imageUrl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get image path
|
|
|
|
let index = imageUrl.indexOf('/file');
|
|
|
|
let index2 = imageUrl.indexOf('?');
|
|
|
|
const imagePath = imageUrl.substring(index + 5, index2);
|
|
|
|
// change image url
|
|
|
|
innerNode.data.src = serviceURL + '/view-image-via-share-link/?token=' + sharedToken + '&path=' + imagePath;
|
|
|
|
}
|
|
|
|
return innerNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifyValueBeforeRender = (value) => {
|
|
|
|
let nodes = value.document.nodes;
|
|
|
|
let newNodes = Utils.changeMarkdownNodes(nodes, this.changeImageURL);
|
|
|
|
value.document.nodes = newNodes;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:30:03 +00:00
|
|
|
render() {
|
|
|
|
if (this.state.loading) {
|
2019-01-31 09:37:02 +00:00
|
|
|
return <Loading />;
|
2019-01-04 10:30:03 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="shared-file-view-md">
|
|
|
|
<div className="shared-file-view-md-header d-flex">
|
|
|
|
<React.Fragment>
|
|
|
|
<a href={siteRoot}>
|
|
|
|
<img src={mediaUrl + logoPath} height={logoHeight} width={logoWidth} title={siteTitle} alt="logo" />
|
|
|
|
</a>
|
|
|
|
</React.Fragment>
|
|
|
|
{ loginUser && <Account /> }
|
|
|
|
</div>
|
|
|
|
<div className="shared-file-view-md-main">
|
|
|
|
<div className="shared-file-view-head">
|
|
|
|
<div className="float-left">
|
|
|
|
<h2 className="ellipsis" title={fileName}>{fileName}</h2>
|
|
|
|
<p className="share-by ellipsis">{gettext('Shared by:')}{' '}{sharedBy}</p>
|
|
|
|
</div>
|
|
|
|
{download &&
|
|
|
|
<div className="float-right">
|
|
|
|
{(loginUser && loginUser !== sharedBy) &&
|
|
|
|
<Button color="secondary" id="save" className="shared-file-op-btn"
|
|
|
|
onClick={this.handleSaveSharedFileDialog}>{gettext('Save as ...')}
|
|
|
|
</Button>
|
|
|
|
}{' '}
|
2019-01-31 09:37:02 +00:00
|
|
|
{(trafficOverLimit === 'False') &&
|
2019-01-04 10:30:03 +00:00
|
|
|
<Button color="success" className="shared-file-op-btn">
|
|
|
|
<a href="?dl=1">{gettext('Download')}({Utils.bytesToSize(fileSize)})</a>
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<div className="shared-file-view-body">
|
2019-01-07 04:38:33 +00:00
|
|
|
<div className="md-view">
|
2019-01-31 09:37:02 +00:00
|
|
|
<MarkdownViewer
|
|
|
|
markdownContent={this.state.markdownContent}
|
|
|
|
showTOC={false}
|
|
|
|
serviceURL={serviceURL}
|
|
|
|
sharedToken={sharedToken}
|
|
|
|
repoID={repoID}
|
|
|
|
modifyValueBeforeRender={this.modifyValueBeforeRender}
|
2019-01-11 08:40:50 +00:00
|
|
|
/>
|
2019-01-04 10:30:03 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{this.state.showSaveSharedFileDialog &&
|
|
|
|
<SaveSharedFileDialog
|
|
|
|
repoID={repoID}
|
|
|
|
sharedToken={sharedToken}
|
|
|
|
toggleCancel={this.toggleCancel}
|
|
|
|
handleSaveSharedFile={this.handleSaveSharedFile}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableWatermark) {
|
|
|
|
let watermark_txt;
|
|
|
|
if (loginUser) {
|
2019-01-31 09:37:02 +00:00
|
|
|
watermark_txt = siteName + ' ' + loginUser;
|
2019-01-04 10:30:03 +00:00
|
|
|
} else {
|
2019-01-31 09:37:02 +00:00
|
|
|
watermark_txt = gettext('Anonymous User');
|
2019-01-04 10:30:03 +00:00
|
|
|
}
|
2019-01-07 04:38:33 +00:00
|
|
|
watermark.init({
|
|
|
|
watermark_txt: watermark_txt,
|
|
|
|
watermark_alpha: 0.075
|
|
|
|
});
|
2019-01-04 10:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render (
|
|
|
|
<SharedFileViewMarkdown />,
|
|
|
|
document.getElementById('wrapper')
|
|
|
|
);
|