Skip to content

Commit

Permalink
Use file extensions (#27)
Browse files Browse the repository at this point in the history
* Add backend support for file extensions

* add file extension to api

* use file extension in client

* use dotenv in client

* fix client

* use file extension in frontend
  • Loading branch information
Kruemmelspalter authored Jan 3, 2023
1 parent 1da09ee commit 250214b
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ class DocumentController {
@RequestParam renderer: String?,
@RequestParam editor: String?,
@RequestParam tags: List<String>?,
@RequestParam mimeType: String?
@RequestParam mimeType: String?,
@RequestParam fileExtension: String?,
): String {
if (mimeType == null && file == null) throw ResponseStatusException(HttpStatus.BAD_REQUEST)
val uuid = if (file != null)
documentService!!.createDocument(title, renderer, editor, mimeType ?: file.contentType, tags, file.inputStream)
else documentService!!.createDocument(title, renderer, editor, mimeType!!, tags, null)
documentService!!.createDocument(title, renderer, editor, mimeType ?: file.contentType, tags, fileExtension, file.inputStream)
else documentService!!.createDocument(title, renderer, editor, mimeType!!, tags, fileExtension, null)
return uuid.toString()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface DocumentDao {
renderer: String,
editor: String,
mimeType: String,
tags: List<String>
tags: List<String>,
fileExtension: String?,
)

fun deleteDocument(id: UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ class DocumentRepository : DocumentDao {
renderer: String,
editor: String,
mimeType: String,
tags: List<String>
tags: List<String>,
fileExtension: String?,
) {

if (jdbcTemplate!!.update(
"insert into Document (id, title, renderer, editor, mimeType) values (?,?,?,?,?)",
"insert into Document (id, title, renderer, editor, mimeType, fileExtension) values (?,?,?,?,?,?)",
uuid.toString(),
title,
renderer,
editor,
mimeType
mimeType,
fileExtension
) != 1
) throw RuntimeException("document creation did not work / didn't affect one row")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Renderer {
companion object {
private val plainRenderer = Renderer().output {
RenderedDocument(
FileInputStream(it.fsService.getFileFromID(it.document.id)),
FileInputStream(it.fsService.getFileFromID(it.document.id, it.document.fileExtension)),
it.document.mimeType,
it.fsService.getFileAttributesFromID(it.document.id).size(),
it.fsService.getFileAttributesFromID(it.document.id, it.document.fileExtension).size(),
it.document.id.toString() + if (it.document.fileExtension != "") "." + it.document.fileExtension else ""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ data class DocumentMeta(
val modified: Timestamp,
val accessed: Timestamp,
val tags: List<String>,
val fileExtension: String?,
) {
constructor(
doc: Document,
created: Timestamp,
modified: Timestamp,
accessed: Timestamp,
tags: List<String>
) : this(doc.id, doc.title, doc.added, doc.editor, created, modified, accessed, tags)
) : this(doc.id, doc.title, doc.added, doc.editor, created, modified, accessed, tags, doc.fileExtension)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DocumentService {
}

private fun documentToMeta(document: Document): DocumentMeta {
val attributes = fsService!!.getFileAttributesFromID(document.id)
val attributes = fsService!!.getFileAttributesFromID(document.id, document.fileExtension)
return DocumentMeta(
document,
Timestamp(attributes.creationTime().toMillis()),
Expand Down Expand Up @@ -64,6 +64,7 @@ class DocumentService {
editor: String?,
mimeType: String,
tags: List<String>?,
fileExtension: String?,
content: InputStream?,
): UUID {
val uuid = uuidGenerator.generate()
Expand All @@ -75,11 +76,12 @@ class DocumentService {
editor ?: "text",
mimeType,
tags ?: listOf(),
fileExtension,
)

fsService!!.createDocument(uuid)
fsService!!.createDocument(uuid, fileExtension)
if (content != null) {
fsService!!.writeToDocument(uuid, content)
fsService!!.writeToDocument(uuid, fileExtension, content)
content.close()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ class FileSystemService {
if (!Files.isWritable(tmpDirectory)) throw Exception("Temporary Directory isn't writable")
}

fun getDocumentPathFromID(id: UUID): Path {
return Paths.get(getDirectoryPathFromID(id).toString(), id.toString())
fun getDocumentPathFromID(id: UUID, fileExtension: String?): Path {
return Paths.get(getDirectoryPathFromID(id).toString(), id.toString() + if (fileExtension != null) ".$fileExtension" else "")
}

fun getDirectoryPathFromID(id: UUID): Path {
return Paths.get(documentDirectory.toString(), id.toString()).toAbsolutePath()
}

fun getFileFromID(id: UUID): File {
return getDocumentPathFromID(id).toFile()
fun getFileFromID(id: UUID, fileExtension: String?): File {
return getDocumentPathFromID(id, fileExtension).toFile()
}

fun getInputStreamFromID(id: UUID): InputStream {
return FileInputStream(getFileFromID(id))
fun getInputStreamFromID(id: UUID, fileExtension: String?): InputStream {
return FileInputStream(getFileFromID(id, fileExtension))
}

fun getContentFromID(id: UUID): String {
fun getContentFromID(id: UUID, fileExtension: String?): String {
val resultBuilder = StringBuilder()
val br = BufferedReader(FileReader(getFileFromID(id)))
val br = BufferedReader(FileReader(getFileFromID(id, fileExtension)))
br.use {
var line = br.readLine()
while (line != null) {
Expand All @@ -69,8 +69,8 @@ class FileSystemService {
return resultBuilder.toString()
}

fun getFileAttributesFromID(id: UUID): BasicFileAttributes {
return Files.readAttributes(getDocumentPathFromID(id), BasicFileAttributes::class.java)
fun getFileAttributesFromID(id: UUID, fileExtension: String?): BasicFileAttributes {
return Files.readAttributes(getDocumentPathFromID(id, fileExtension), BasicFileAttributes::class.java)
}

fun getTemporaryDirectory(): Path {
Expand All @@ -87,15 +87,15 @@ class FileSystemService {
else FileInputStream(file)
}

fun createDocument(id: UUID) {
fun createDocument(id: UUID, fileExtension: String?) {
val directory = getDirectoryPathFromID(id).toFile()
if (!directory.mkdir()) throw Error("could not create document directory")
val file = getFileFromID(id)
val file = getFileFromID(id, fileExtension)
if (!file.createNewFile()) throw Error("could not create document file")
}

fun writeToDocument(id: UUID, stream: InputStream) {
val outStream = FileOutputStream(getFileFromID(id))
fun writeToDocument(id: UUID, fileExtension: String?, stream: InputStream) {
val outStream = FileOutputStream(getFileFromID(id, fileExtension))
stream.transferTo(outStream)
outStream.close()
}
Expand Down
2 changes: 1 addition & 1 deletion backend/app/src/main/resources/sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ create table Document (
renderer varchar(16) default 'mimeSpecific',
editor varchar(32) default 'mimeSpecific',
mimeType varchar(64) not null,
fileExtension varchar(10) default '',
fileExtension varchar(10),
primary key (id)
);

Expand Down
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
17 changes: 9 additions & 8 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ const express = require('express')
const proxy = require('express-http-proxy')
const { execSync, spawn } = require("child_process")
const axios = require('axios')
require('dotenv').config()

const API_HOST = 'http://172.31.69.5'
const MOUNT_PATH = process.env.HOME + '/.filespider'
const PORT = 8080

const API_HOST = process.env.API_HOST || 'http://172.31.69.4'
const MOUNT_PATH = process.env.MOUNT_PATH || (process.env.HOME + '/.filespider')
const PORT = process.env.PORT || 8080
const app = express()

const editors = {
mimeSpecific: path => ({explorer: true, command: 'xdg-open', args: [path]}),
mime: path => ({explorer: true, command: 'xdg-open', args: [path]}),
plain: path => ({explorer: true, command: 'kate', args: [path]}),
xournalpp: path => ({explorer: true, command: 'xournalpp', args: [path]}),
}
Expand All @@ -34,13 +34,14 @@ app.post('/document/:docId/edit', async (req, res) => {
res.status(500).send({path: `${API_HOST}/document/${req.params.docId}`, error: e})
return
}
const path = `${MOUNT_PATH}/${req.params.docId}/${req.params.docId}`
const editor = editors[meta.data.editor](path)
if(editor === undefined) {
const path = `${MOUNT_PATH}/${req.params.docId}/${req.params.docId}` + (meta.data.fileExtension !== undefined ? "." + meta.data.fileExtension : "")
const editorProvider = editors[meta.data.editor]
if(editorProvider === undefined) {
res.status(400).send(`Wrong editor ${meta.data.editor}`)
console.log(`Wrong editor ${meta.data.editor}`)
return
}
const editor = editorProvider(path)
if(editor.explorer) spawn('dolphin', [`${path}/..`])
spawn(editor.command, editor.args)
res.send()
Expand Down
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.2.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-http-proxy": "^1.6.3"
},
Expand Down
5 changes: 5 additions & 0 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ destroy@1.2.0:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.2.0.tgz#4803735509ad8be552934c67df614f94e66fa015"
integrity sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==

dotenv@^16.0.3:
version "16.0.3"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==

ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down
12 changes: 11 additions & 1 deletion frontend/components/DocumentCreationDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/>
<v-text-field v-model="creationMeta.renderer" label="Renderer (mime specific by default)" />
<v-text-field v-model="creationMeta.editor" label="Editor (mime specific by default)" />
<v-text-field v-model="creationMeta.fileExtension" label="File Extension (auto from file name by default)" />
<v-btn type="submit">
Submit
</v-btn>
Expand Down Expand Up @@ -67,6 +68,7 @@ export default {
renderer: null,
editor: null,
file: null,
fileExtension: null,
},
}
},
Expand All @@ -92,7 +94,14 @@ export default {
formData.append('editor', this.creationMeta.editor || 'mime')
if (this.creationMeta.file !== null) {
formData.append('file', this.creationMeta.file)
if (this.creationMeta.fileExtension === null) {
formData.append('fileExtension', this.creationMeta.file.name.split('.').at(-1))
}
}
if (this.creationMeta.fileExtension !== null) {
formData.append('fileExtension', this.creationMeta.fileExtension)
}
this.$axios.$post(`${this.apiSource}/document/`, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
Expand All @@ -106,7 +115,8 @@ export default {
show () {
this.visible = true
},
},
}
,
}
</script>
<style scoped>
Expand Down

0 comments on commit 250214b

Please sign in to comment.