Skip to content

Commit

Permalink
upload of modified open file WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Apr 24, 2021
1 parent 4ff7e08 commit a747413
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 15 deletions.
6 changes: 3 additions & 3 deletions bin/push-docker-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ fi
VERSION=$1
SERIES=${VERSION:0:4}-latest

sudo docker push zadam/trilium:$VERSION
sudo docker push zadam/trilium:$SERIES
docker push zadam/trilium:$VERSION
docker push zadam/trilium:$SERIES

if [[ $1 != *"beta"* ]]; then
sudo docker push zadam/trilium:latest
docker push zadam/trilium:latest
fi
123 changes: 117 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"axios": "0.21.1",
"better-sqlite3": "7.1.4",
"body-parser": "1.19.0",
"chokidar": "^3.5.1",
"cls-hooked": "4.2.2",
"commonmark": "0.29.3",
"cookie-parser": "1.4.5",
Expand Down Expand Up @@ -56,7 +57,7 @@
"jsdom": "16.5.3",
"mime-types": "2.1.30",
"multer": "1.4.2",
"node-abi": "2.21.0",
"node-abi": "2.26.0",
"open": "8.0.6",
"portscanner": "2.2.0",
"rand-token": "1.0.1",
Expand All @@ -79,7 +80,7 @@
},
"devDependencies": {
"cross-env": "7.0.3",
"electron": "13.0.0-beta.16",
"electron": "13.0.0-beta.17",
"electron-builder": "22.10.5",
"electron-packager": "15.2.0",
"electron-rebuild": "2.3.5",
Expand Down
45 changes: 41 additions & 4 deletions src/public/app/widgets/type_widgets/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import utils from "../../services/utils.js";
import openService from "../../services/open.js";
import TypeWidget from "./type_widget.js";
import fileWatcher from "../../services/file_watcher.js";
import server from "../../services/server.js";

const TPL = `
<div class="note-detail-file note-detail-printable">
Expand All @@ -23,6 +24,12 @@ const TPL = `
}
</style>
<div class="file-watcher-wrapper alert alert-warning">
<p>File <code class="file-watcher-path"></code> has been last modified on <span class="file-watcher-last-modified"></span>.</p>
<button class="btn btn-sm file-watcher-upload-button">Upload modified file</button>
</div>
<pre class="file-preview-content"></pre>
<div class="file-preview-not-available alert alert-info">
Expand All @@ -47,12 +54,25 @@ export default class FileTypeWidget extends TypeWidget {
this.$pdfPreview = this.$widget.find(".pdf-preview");
this.$videoPreview = this.$widget.find(".video-preview");
this.$audioPreview = this.$widget.find(".audio-preview");

this.$fileWatcherWrapper = this.$widget.find(".file-watcher-wrapper");
this.$fileWatcherWrapper.hide();

this.$fileWatcherPath = this.$widget.find(".file-watcher-path");
this.$fileWatcherLastModified = this.$widget.find(".file-watcher-last-modified");
this.$fileWatcherUploadButton = this.$widget.find(".file-watcher-upload-button");

this.$fileWatcherUploadButton.on("click", async () => {
await server.post(`notes/${this.noteId}/upload-modified-file`, {
filePath: this.$fileWatcherPath.text()
});

fileWatcher.fileModificationUploaded(this.noteId);
this.refreshFileWatchingStatus();
});
}

async doRefresh(note) {
const attributes = note.getAttributes();
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);

this.$widget.show();

const noteComplement = await this.tabContext.getNoteComplement();
Expand Down Expand Up @@ -87,5 +107,22 @@ export default class FileTypeWidget extends TypeWidget {
else {
this.$previewNotAvailable.show();
}

this.refreshFileWatchingStatus();
}

refreshFileWatchingStatus() {
const status = fileWatcher.getFileModificationStatus(this.noteId);

this.$fileWatcherWrapper.toggle(!!status);

if (status) {
this.$fileWatcherPath.text(status.filePath);
this.$fileWatcherLastModified.text(dayjs.unix(status.lastModifiedMs / 1000).format("HH:mm:ss"));
}
}

openedFileUpdatedEvent(data) {
this.refreshFileWatchingStatus();
}
}
15 changes: 15 additions & 0 deletions src/routes/api/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const noteRevisionService = require('../../services/note_revisions');
const tmp = require('tmp');
const fs = require('fs');
const { Readable } = require('stream');
const chokidar = require('chokidar');
const ws = require('../../services/ws');

function updateFile(req) {
const {noteId} = req.params;
Expand Down Expand Up @@ -120,6 +122,19 @@ function saveToTmpDir(req) {
fs.writeSync(tmpObj.fd, note.getContent());
fs.closeSync(tmpObj.fd);

if (utils.isElectron()) {
chokidar.watch(tmpObj.name).on('change', (path, stats) => {
ws.sendMessageToAllClients({
type: 'openedFileUpdated',
noteId: noteId,
lastModifiedMs: stats.atimeMs,
filePath: tmpObj.name
});

console.log(stats, path);
});
}

return {
tmpFilePath: tmpObj.name
};
Expand Down
2 changes: 2 additions & 0 deletions src/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ function formatDownloadTitle(filename, type, mime) {
filename = "untitled";
}

filename = sanitize(filename);

if (type === 'text') {
return filename + '.html';
} else if (['relation-map', 'search'].includes(type)) {
Expand Down

0 comments on commit a747413

Please sign in to comment.