Skip to content

Commit 0731722

Browse files
committed
Enable ZIP uploads
1 parent a3386ed commit 0731722

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

app.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from urllib.parse import urlparse
1111

1212
LOG_FILE = 'app.log'
13-
IMPORTER_URL ='http://importer:5020'
14-
FUSEKI_URL ='http://fuseki:3030'
13+
IMPORTER_HOST = 'http://importer:5020'
14+
FUSEKI_HOST = 'http://fuseki:3030'
1515

1616

1717
logging.basicConfig(filename=LOG_FILE, level=logging.INFO)
@@ -86,7 +86,7 @@ def logout():
8686
@app.route('/fuseki', methods=['post'])
8787
def fuseki():
8888
if data := request.json.get('data'):
89-
res = requests.post(f'{FUSEKI_URL}/n4o?query={data}')
89+
res = requests.post(f'{FUSEKI_HOST}/n4o?query={data}')
9090
return jsonify(res.text), res.status_code
9191

9292

@@ -95,7 +95,7 @@ def importer(subpath):
9595
'''Forwards requests to the importer service'''
9696
res = requests.request(
9797
method=request.method,
98-
url=f'{IMPORTER_URL}/{subpath}', # Forward to importer service
98+
url=f'{IMPORTER_HOST}/{subpath}', # Forward to importer service
9999
headers={k: v for k, v in request.headers if k.lower() != 'host'}, # Exclude 'host' header
100100
data=request.get_data(),
101101
cookies=request.cookies,
@@ -110,21 +110,22 @@ def importer(subpath):
110110
return response
111111

112112

113-
@app.route('/postCollectionData', methods=['POST'])
114-
def postCollectionData():
115-
'''Posts new collection data. Recent data will be deleted.'''
116-
if uri := request.json.get('uri'):
117-
index = urlparse(uri).path.strip('/').split('/')[-1]
118-
if data := request.json['data']:
113+
@app.route('/postCollectionData/<int:coll_index>', methods=['POST'])
114+
def postCollectionData(coll_index):
115+
'''Upload new collection data. Recent data will be deleted.'''
116+
if coll_index > 0:
117+
if file := request.files.get('file'):
119118
# Copy data to file, then call importer services receive and add
120-
suffix = request.json.get("suffix", "ttl")
121-
import_file = f'import_{index}.{suffix}'
122-
Path(f'./data').mkdir(exist_ok=True)
123-
Path(f'./data/{import_file}').write_text(data)
124-
service = f'{IMPORTER_URL}/collection/{index}'
125-
res = requests.post(f'{service}/receive?from={import_file}')
126-
res = requests.post(f'{service}/load')
127-
#res = requests.post(f'{service}/add')
119+
# logger.info(f'name = {file.filename} index = {index}')
120+
buffer_name = f'collection_{coll_index}{Path(file.filename).suffix}'
121+
file.save(f'./data/{buffer_name}')
122+
123+
service_url = f'{IMPORTER_HOST}/collection/{coll_index}'
124+
res = requests.post(f'{service_url}/receive?from={buffer_name}')
125+
if res.ok:
126+
res = requests.post(f'{service_url}/load')
127+
# TODO res = requests.post(f'{service}/add') #Not supporte
128+
Path(f'./data/{buffer_name}').unlink(missing_ok=False)
128129
return res.text, res.status_code
129130
return jsonify(message='No data or collection index provided')
130131

static/collections.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,22 @@ function showProgess(on = false) {
5050
}
5151

5252
/// Import TTL data into the KG
53-
function postCollectionData(uri, data, suffix) {
53+
function postCollectionData(index,file) {
5454
showProgess(true);
55-
var req = {
56-
method: 'POST',
57-
headers: {
58-
'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json'
59-
},
60-
body: JSON.stringify({ "data": data, "uri": uri, suffix: suffix })
61-
}
62-
fetch('/postCollectionData', req)
55+
var data = new FormData();
56+
data.append('file', file, file.filename);
57+
data.append('index', index);
58+
59+
var req = { method: 'POST', body: data, };
60+
fetch('/postCollectionData/'+index, req)
6361
.then(response => response.json())
64-
.then(json => console.log(json))
65-
.catch(err => console.error(err))
62+
.then(json => {
63+
console.log(json);
64+
if (json.code) {
65+
alert(`Error ${json.code}:\n\t${json.message} at line ${json.position.linecol}`);
66+
}
67+
})
68+
.catch(err => alert(err))
6669
.finally(() => {
6770
showProgess(false);
6871
});
@@ -170,12 +173,8 @@ function makeAppData() {
170173
}
171174
else {
172175
const file0 = this.rdfFiles[0]
173-
const textPromise = file0.text();
174-
textPromise.then((data) => {
175-
let suffix = file0.name.split('.').pop().toLowerCase();
176-
postCollectionData(this.displayCollection.uri, data, suffix);
177-
this.rdfFiles = [];
178-
});
176+
postCollectionData(this.displayCollection.id, file0);
177+
this.rdfFiles = [];
179178
}
180179
},
181180
putCollection(collection) {

templates/collections.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h5 class="modal-title" id="uploadRDFLabel">Uploading RDF files</h5>
3333
<div class="modal-body">
3434
<form novalidate @submit.prevent="uploadRDF">
3535
<div class="input-group mb-3">
36-
<input type="file" class="form-control" @change="saveRDFUrl" accept=".ttl,.nt,.xml,.n3" />&nbsp;
36+
<input type="file" class="form-control" @change="saveRDFUrl" accept=".ttl,.nt,.xml,.n3,.zip" />&nbsp;
3737
<input type="submit" class="btn btn-primary" data-bs-dismiss="modal" value="Open" />
3838
</div>
3939
</form>

0 commit comments

Comments
 (0)