Skip to content

Commit

Permalink
Upload directory of images
Browse files Browse the repository at this point in the history
We can now upload directory containing the images we want to upload.
They'll be added to the server's `uploads/` directory.

Closes scorelab#272
  • Loading branch information
2knal committed Feb 23, 2020
1 parent 73f85c5 commit c113875
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 113 deletions.
4 changes: 3 additions & 1 deletion labellab-client/src/components/labeller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ class LabelingLoader extends Component {
labels={lab}
labelData={(img && img.labelData) || {}}
imageUrl={
'http://' +
process.env.REACT_APP_HOST +
':' +
process.env.REACT_APP_SERVER_PORT +
`/static/uploads/${image.imageUrl}?${Date.now()}`
(image.directory.length > 0 ? `/static/uploads/${image.directory}/${image.imageUrl}?${Date.now()}` :`/static/uploads/${image.imageUrl}?${Date.now()}`)
}
demo={false}
{...props}
Expand Down
89 changes: 77 additions & 12 deletions labellab-client/src/components/project/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class ImagesIndex extends Component {
imageName: '',
projectId: '',
showform: false,
format: ''
format: '',
directoryName: '',
directoryImageNames: [],
directoryImageFormats: [],
directoryImages: []
}
}
handleImageChange = e => {
Expand All @@ -34,19 +38,63 @@ class ImagesIndex extends Component {
}
reader.readAsDataURL(file)
}
handleAddDirectory = e => {
e.preventDefault()
const directoryInput = document.getElementById("directory-embedpollfileinput")

Array.from(directoryInput.files).forEach(file => {
const reader = new FileReader()
// Validating file type to be image
const directoryName = file.webkitRelativePath.split('/')[0]
if (file.type.startsWith('image')) {
reader.onloadend = () => {
this.setState({
directoryName,
directoryImages: [...this.state.directoryImages, reader.result],
directoryImageNames: [...this.state.directoryImageNames, file.name],
directoryImageFormats: [...this.state.directoryImageFormats, file.type]
})
}
reader.readAsDataURL(file)
}
});
this.setState({
showform: !this.state.showform
})
}
handleSubmit = e => {
e.preventDefault()
const { project, fetchProject, submitImage } = this.props
const { imageName, image, format } = this.state
let data = {
imageName: imageName,
image: image,
projectId: project.projectId,
format: format
const { directoryName, directoryImageNames, directoryImages, directoryImageFormats } = this.state
let data = {}
if (this.state.imageName) {
data = {
imageName,
image,
projectId: project.projectId,
format
}
} else if (this.state.directoryName) {
data = {
directoryName,
directoryImages,
directoryImageNames,
directoryImageFormats,
projectId: project.projectId
}
}
submitImage(data, () => {
this.setState({
showform: false
showform: false,
image: '',
file: '',
format: '',
imageName: '',
directoryImageNames: [],
directoryImages: [],
directoryImageFormats: [],
directoryName: ''
})
fetchProject(project.projectId)
})
Expand Down Expand Up @@ -94,18 +142,33 @@ class ImagesIndex extends Component {
Add Image
</label>
</div>

<div>
<input
type="file"
webkitdirectory="true"
mozdirectory="true"
onChange={this.handleAddDirectory}
className="image-file-input"
id="directory-embedpollfileinput"
/>
<label
htmlFor="directory-embedpollfileinput"
className="ui medium primary left floated button custom-margin"
>
Add Directory
</label>
</div>
{showform ? (
<Form
className="file-submit-form"
encType="multiple/form-data"
onSubmit={this.handleSubmit}
>
<Form.Field>
<label>Image Name</label>
<label>{this.state.imageName ? 'Image': 'Directory'} Name</label>
<input
name="imageName"
value={imageName}
name={this.state.imageName ? 'imageName': 'directoryName'}
value={this.state.imageName ? this.state.imageName: this.state.directoryName}
onChange={this.handleChange}
placeholder="Image Name"
/>
Expand All @@ -125,6 +188,7 @@ class ImagesIndex extends Component {
<Table.Header className="image-table-header">
<Table.Row className="flex image-table-row-back">
<Table.HeaderCell style={columnStyles[0]}>ID</Table.HeaderCell>
<Table.HeaderCell style={columnStyles[1]}>Directory</Table.HeaderCell>
<Table.HeaderCell style={columnStyles[1]}>
Image Link
</Table.HeaderCell>
Expand Down Expand Up @@ -201,14 +265,15 @@ const columnStyles = [
const Row = ({ image, projectId, style, onDelete, imageId }) => (
<Table.Row style={{ ...style, display: 'flex' }}>
<Table.Cell style={columnStyles[0]}>{imageId + 1}</Table.Cell>
<Table.Cell style={columnStyles[1]}><i class="folder icon"></i>{ image.directory ? image.directory: null }</Table.Cell>
<Table.Cell style={columnStyles[1]}>
<a
href={
'http://' +
process.env.REACT_APP_HOST +
':' +
process.env.REACT_APP_SERVER_PORT +
`/static/uploads/${image.imageUrl}?${Date.now()}`
(image.directory.length > 0 ? `/static/uploads/${image.directory}/${image.imageUrl}?${Date.now()}` :`/static/uploads/${image.imageUrl}?${Date.now()}`)
}
>
{image.imageName}
Expand Down
4 changes: 2 additions & 2 deletions labellab-server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ if (process.env.GITHUB_CLIENT_ID) {

app.use(logger('dev'))
app.use(cors())
app.use(express.json({ limit: '10mb' }))
app.use(express.urlencoded({ extended: false, limit: '10mb' }))
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ extended: true, limit: '50mb' }))
app.use(cookieParser())
app.set('/views', path.join(__dirname, 'views'))
app.engine('html', require('ejs').renderFile)
Expand Down
Loading

0 comments on commit c113875

Please sign in to comment.