michael / hub

Substance Hub
6 stars 0 forks source link

Blobs and such #62

Closed michael closed 11 years ago

michael commented 11 years ago

Integrated your blob API with the Composer. Currently, I'm pushing the blobs one by one. Once they are all transfered, the actual version gets created.

  createVersion: function(cb) {
    var doc = this.document;
    var that = this;
    var data = doc.toJSON(true);

    var funcs = [];
    var blobs = {};
    function pushBlob(docId, blobId, blobData) {
      if (blobs[blobId]) return; // skip -> already queued
      blobs[blobId] = true;
      funcs.push(function(data, cb) {
        that.client.createBlob(docId, blobId, blobData, cb);
      });
    }

    // Push document cover?
    if (doc.properties.cover_medium) {
      pushBlob(doc.id, doc.properties.cover_medium, that.getBlob(doc.properties.cover_medium));
      pushBlob(doc.id, doc.properties.cover_large, that.getBlob(doc.properties.cover_large));
    }

    // Find all images
    _.each(doc.nodes, function(node) {
      if (node.type === "image") {
        pushBlob(doc.id, node.medium, that.getBlob(node.medium));
        pushBlob(doc.id, node.large, that.getBlob(node.large));
      }
    });

    // Push blobs first
    Substance.util.async(funcs, null, function(err) {
      if (err) return cb(err);

      // Now create version on the server
      that.client.createVersion(doc.id, data, function(err) {
        if (err) return cb(err);
        doc.meta.published_at = new Date();
        doc.meta.published_commit = doc.getRef('head');

        that.updateMeta(function() {
          that.loadPublications(cb);
        });
      });
    });

  },

However, we need to be careful not to mix up our Publishing Blob API with the Document API (used for replication).

In my opinion /documents/:docId/blob/:blobId should rather be the endpoint for the Replicator rather than being used in conjunction with creating versions for published docs.

The more I think about it, the more I think we should do the whole create new version dance in a single request as an atomic operation. That way we don't run into rollback troubles if it happens to be that there's an error somewhere in between. Also there wouldn't be confusion with the real "Blobs API" (being a part of Substance.Store API)

michael commented 11 years ago

Done.