From 03a23675324469cdf90c1adc91e868c3234af6c1 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 31 Oct 2021 21:15:18 +0000 Subject: [PATCH] Fixed lots of things about storing blobs. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3680 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/ssb.js | 19 ++++++++++++++++--- src/ssb.db.c | 18 +++++++++++++++--- src/ssb.js.c | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/core/ssb.js b/core/ssb.js index 7ffa071e..22e04942 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -92,13 +92,27 @@ ssb.registerConnectionsChanged(function(change, connection) { } } else { debug_print("blobs.get", id); + var received_bytes = 0; + var expected_bytes = message.message[id]; + var buffer = new Uint8Array(expected_bytes); connection.send_json({'name': ['blobs', 'get'], 'type': 'source', 'args': [id]}, function(message) { - debug_print(id, '=>', debug_utf8Decode(message.message)); - ssb.blobStore(message.message); + buffer.set(new Uint8Array(message.message, 0, message.message.byteLength), received_bytes); + received_bytes += message.message.byteLength; + if (received_bytes == expected_bytes) { + ssb.blobStore(buffer); + } }); } }); }); + followingDeep(g_database, [ssb.whoami()], 2).then(function(ids) { + for (let id of ids) { + var sequence = get_latest_sequence_for_author(id); + connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': id, 'seq': sequence}]}, function(message) { + ssb.storeMessage(message.message.value); + }); + } + }); } else if (change == 'remove') { debug_print('REMOVE', connection.id); delete g_wants_requests[connection.id]; @@ -110,7 +124,6 @@ ssb.registerConnectionsChanged(function(change, connection) { ssb.registerRpc(['blobs', 'createWants'], function(request) { g_wants_requests[request.connection.id] = request; function blob_want_discovered(id) { - debug_print('discovered', id); var message = {}; message[id] = -1; request.send_json(message); diff --git a/src/ssb.db.c b/src/ssb.db.c index 948f609c..673441e8 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -218,10 +218,8 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* char id[512]; snprintf(id, sizeof(id), "&%s.sha256", hash64); - printf("blob store %s\n", id); - const char* query = "INSERT INTO blobs (id, content, created) VALUES ($1, $2, CAST(strftime('%s') AS INTEGER)) ON CONFLICT DO NOTHING"; - if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) + if (sqlite3_prepare(db, "INSERT INTO blobs (id, content, created) VALUES ($1, $2, CAST(strftime('%s') AS INTEGER)) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_blob(statement, 2, blob, size, NULL) == SQLITE_OK) @@ -239,6 +237,20 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* printf("prepare failed: %s\n", sqlite3_errmsg(db)); } + printf("blob store %s %zd => %d\n", id, size, result); + + if (result) + { + if (sqlite3_prepare(db, "DELETE FROM blob_wants WHERE id = ?1", -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK) + { + sqlite3_step(statement); + } + sqlite3_finalize(statement); + } + } + if (result && out_id) { snprintf(out_id, out_id_size, "%s", id); diff --git a/src/ssb.js.c b/src/ssb.js.c index e273e21b..206183ce 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -97,6 +97,24 @@ static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int result = JS_NewString(context, id); } } + else + { + size_t offset; + size_t element_size; + JSValue buffer = tf_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size); + if (!JS_IsException(buffer)) + { + blob = tf_try_get_array_buffer(context, &size, buffer); + if (blob) + { + if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id))) + { + result = JS_NewString(context, id); + } + } + } + JS_FreeValue(context, buffer); + } } return result; }