class OrientPyPiFacetImpl extends FacetSupport implements PyPiFacet {

  @Override
  public Asset putPackage(final String path, final AssetBlob assetBlob) throws IOException {
    final StorageTx tx = UnitOfWork.currentTx();
    final Bucket bucket = tx.findBucket(getRepository());

    Asset asset = findAsset(tx, bucket, path);

    if (asset == null) {
      final String filename = extractFilenameFromPath(path);

      Map<String, String> attributes;
      try (InputStream is = assetBlob.getBlob().getInputStream()) {
        attributes = extractMetadata(is);
      }

      if (!attributes.containsKey(P_NAME)) {
        log.debug("No name found in metadata for {}, extracting from filename.", filename);
        attributes.put(P_NAME, normalizeName(extractNameFromFilename(filename)));
      }
      if (!attributes.containsKey(P_VERSION)) {
        log.debug("No version found in metadata for {}, extracting from filename.", filename);
        attributes.put(P_VERSION, extractVersionFromFilename(filename));
      }

      final String name = attributes.get(P_NAME);
      final String version = attributes.get(P_VERSION);

      Component  component = findComponent(tx, getRepository(), name, version);
      if (component == null) {
        component = tx.createComponent(bucket, getRepository().getFormat());
        component.name(name);
        component.version(version);
        component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY));
        tx.saveComponent(component);
      }
      asset = tx.createAsset(bucket, component);
      asset.name(path);
      copyAttributes(asset, attributes);
      saveAsset(tx, asset, assetBlob, AssetKind.PACKAGE);
    }

    return asset;
  }

  @Inject public  OrientPyPiFacetImpl(final AssetEntityAdapter assetEntityAdapter);

}