Skip to content

Commit

Permalink
Merge pull request #59 from djedi/export
Browse files Browse the repository at this point in the history
Export
  • Loading branch information
m0ngr31 authored Nov 26, 2021
2 parents 78e8ee2 + e1b8f9c commit c322f27
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,6 @@ test.py

# Docker volume
/dailynotes-volume

# Generated exports
export.zip
23 changes: 22 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import zipfile
from app import app, db, argon2
from app.models import User, Note, Meta, aes_encrypt, aes_encrypt_old
from flask import render_template, request, jsonify, abort
from flask import render_template, request, jsonify, abort, send_file
from flask_jwt_extended import jwt_required, create_access_token, get_jwt_identity


Expand Down Expand Up @@ -396,6 +397,26 @@ def search():
return jsonify(notes=sorted_nodes), 200


@app.route('/api/export')
@jwt_required()
def export():
username = get_jwt_identity()
user = User.query.filter_by(username=username.lower()).first()

if not user:
abort(400)

zf = zipfile.ZipFile('export.zip', mode='w')
notes = user.notes
for note in notes:
ret_note = note.serialize
zf.writestr(ret_note['title'] + '.md', ret_note['data'], zipfile.ZIP_DEFLATED)
print(ret_note)
zf.close()

return send_file('../export.zip', as_attachment=True)


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
Expand Down
6 changes: 6 additions & 0 deletions client/src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
{{ sidebar.autoSave ? 'Disable Auto-Save' : 'Enable Auto-Save' }}
</b-switch>
</b-dropdown-item>
<b-dropdown-item @click="exportNotes()">Export Notes</b-dropdown-item>
<b-dropdown-item @click="logout()">Logout</b-dropdown-item>
</b-dropdown>
</div>
Expand All @@ -104,6 +105,7 @@ import format from 'date-fns/format';
import SidebarInst from '../services/sidebar';
import {clearToken} from '../services/user';
import {NoteService} from '../services/notes';
import {IHeaderOptions} from '../interfaces';
Expand Down Expand Up @@ -189,6 +191,10 @@ export default class Header extends Vue {
this.isSaving = false;
}
public async exportNotes() {
NoteService.exportNotes();
}
public logout() {
clearToken();
this.$router.push({name: 'Login'});
Expand Down
7 changes: 7 additions & 0 deletions client/src/services/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@ export const NoteService = {
*/
deleteNote: async (uuid: string): Promise<void> => {
await Requests.delete(`/delete_note/${uuid}`);
},

/**
* Exports all notes to a zip file and downloads
*/
exportNotes: async (): Promise<void> => {
Requests.download("/export", "export.zip");
}
};
16 changes: 16 additions & 0 deletions client/src/services/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,21 @@ export const Requests = {

delete: (url: string): AxiosPromise => {
return axios.delete(url);
},

download: (url: string, filename: string): void => {
axios({
url: url, // File URL Goes Here
method: "GET",
responseType: "blob"
}).then(res => {
var FILE = window.URL.createObjectURL(new Blob([res.data]));
var docUrl = document.createElement("a");
docUrl.href = FILE;
docUrl.setAttribute("download", filename);
document.body.appendChild(docUrl);
docUrl.click();
document.body.removeChild(docUrl);
});
}
};

0 comments on commit c322f27

Please sign in to comment.