Merge pull request #3952 from overleaf/revert-cmg-binary-file
Revert "Merge pull request #3526 from overleaf/cmg-binary-file" GitOrigin-RevId: 5f539f26992fefd01b07922b1f43a3a3bc753141
This commit is contained in:
committed by
Copybot
parent
1186c3e9a4
commit
4e9d7c36ff
@@ -1,67 +0,0 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
const MAX_FILE_SIZE = 2 * 1024 * 1024
|
||||
|
||||
export default function BinaryFileText({ file, onLoad, onError }) {
|
||||
const [textPreview, setTextPreview] = useState('')
|
||||
const [shouldShowDots, setShouldShowDots] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
let path = `/project/${window.project_id}/file/${file.id}`
|
||||
fetch(path, { method: 'HEAD' })
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error('HTTP Error Code: ' + response.status)
|
||||
return response.headers.get('Content-Length')
|
||||
})
|
||||
.then(fileSize => {
|
||||
let truncated = false
|
||||
let maxSize = null
|
||||
if (fileSize > MAX_FILE_SIZE) {
|
||||
truncated = true
|
||||
maxSize = MAX_FILE_SIZE
|
||||
}
|
||||
|
||||
if (maxSize != null) {
|
||||
path += `?range=0-${maxSize}`
|
||||
}
|
||||
fetch(path)
|
||||
.then(response => {
|
||||
response.text().then(text => {
|
||||
if (truncated) {
|
||||
text = text.replace(/\n.*$/, '')
|
||||
}
|
||||
|
||||
setTextPreview(text)
|
||||
onLoad()
|
||||
setShouldShowDots(truncated)
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
onError()
|
||||
console.error(err)
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
onError()
|
||||
})
|
||||
}, [file.id, onError, onLoad])
|
||||
return (
|
||||
<div>
|
||||
{textPreview && (
|
||||
<div className="text-preview">
|
||||
<div className="scroll-container">
|
||||
<p>{textPreview}</p>
|
||||
{shouldShowDots && <p>...</p>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
BinaryFileText.propTypes = {
|
||||
file: PropTypes.shape({ id: PropTypes.string }).isRequired,
|
||||
onLoad: PropTypes.func.isRequired,
|
||||
onError: PropTypes.func.isRequired
|
||||
}
|
||||
Reference in New Issue
Block a user