This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from loverto:feat-upload
GitOrigin-RevId: 283741b1783a76867d93ea562cb8357d026217fa
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
src/main/java/org/sonatype/repository/helm/HelmUploadHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Sonatype Nexus (TM) Open Source Version | ||
* Copyright (c) 2018-present Sonatype, Inc. | ||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | ||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | ||
* | ||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | ||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | ||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | ||
*/ | ||
package org.sonatype.repository.helm; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import org.sonatype.nexus.repository.Repository; | ||
import org.sonatype.nexus.repository.rest.UploadDefinitionExtension; | ||
import org.sonatype.nexus.repository.security.ContentPermissionChecker; | ||
import org.sonatype.nexus.repository.security.VariableResolverAdapter; | ||
import org.sonatype.nexus.repository.storage.StorageFacet; | ||
import org.sonatype.nexus.repository.storage.TempBlob; | ||
import org.sonatype.nexus.repository.transaction.TransactionalStoreBlob; | ||
import org.sonatype.nexus.repository.upload.ComponentUpload; | ||
import org.sonatype.nexus.repository.upload.UploadDefinition; | ||
import org.sonatype.nexus.repository.upload.UploadHandlerSupport; | ||
import org.sonatype.nexus.repository.upload.UploadResponse; | ||
import org.sonatype.nexus.repository.view.PartPayload; | ||
import org.sonatype.nexus.rest.ValidationErrorsException; | ||
import org.sonatype.repository.helm.internal.HelmFormat; | ||
import org.sonatype.repository.helm.internal.hosted.HelmHostedFacet; | ||
import org.sonatype.repository.helm.internal.metadata.HelmAttributes; | ||
import org.sonatype.repository.helm.internal.util.HelmAttributeParser; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static org.sonatype.repository.helm.internal.util.HelmDataAccess.HASH_ALGORITHMS; | ||
|
||
/** | ||
* Support helm upload for web page | ||
* | ||
* @author yinlongfei | ||
*/ | ||
@Singleton | ||
@Named(HelmFormat.NAME) | ||
public class HelmUploadHandler | ||
extends UploadHandlerSupport | ||
{ | ||
private UploadDefinition definition; | ||
|
||
private final ContentPermissionChecker contentPermissionChecker; | ||
|
||
private final VariableResolverAdapter variableResolverAdapter; | ||
|
||
private final HelmAttributeParser helmPackageParser; | ||
|
||
|
||
@Inject | ||
public HelmUploadHandler(final ContentPermissionChecker contentPermissionChecker, | ||
final HelmAttributeParser helmPackageParser, | ||
@Named("simple") final VariableResolverAdapter variableResolverAdapter, | ||
final Set<UploadDefinitionExtension> uploadDefinitionExtensions) | ||
{ | ||
super(uploadDefinitionExtensions); | ||
this.contentPermissionChecker = contentPermissionChecker; | ||
this.variableResolverAdapter = variableResolverAdapter; | ||
this.helmPackageParser = helmPackageParser; | ||
} | ||
|
||
|
||
@Override | ||
public UploadResponse handle(Repository repository, ComponentUpload upload) throws IOException { | ||
HelmHostedFacet facet = repository.facet(HelmHostedFacet.class); | ||
StorageFacet storageFacet = repository.facet(StorageFacet.class); | ||
|
||
PartPayload payload = upload.getAssetUploads().get(0).getPayload(); | ||
try (TempBlob tempBlob = storageFacet.createTempBlob(payload, HASH_ALGORITHMS)) { | ||
HelmAttributes attributesFromInputStream = helmPackageParser.getAttributesFromInputStream(tempBlob.get()); | ||
|
||
String name = attributesFromInputStream.getName(); | ||
String version = attributesFromInputStream.getVersion(); | ||
|
||
if (StringUtils.isBlank(name)) { | ||
throw new ValidationErrorsException("Metadata is missing the name attribute"); | ||
} | ||
|
||
if (StringUtils.isBlank(version)) { | ||
throw new ValidationErrorsException("Metadata is missing the version attribute"); | ||
} | ||
|
||
String extension = "tgz"; | ||
|
||
String path = name + "-" + version + "." + extension; | ||
|
||
ensurePermitted(repository.getName(), HelmFormat.NAME, path, Collections.EMPTY_MAP); | ||
return TransactionalStoreBlob.operation.withDb(storageFacet.txSupplier()).throwing(IOException.class) | ||
.call(() -> new UploadResponse(facet.upload(path, tempBlob, payload))); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public UploadDefinition getDefinition() { | ||
if (definition == null) { | ||
definition = getDefinition(HelmFormat.NAME, false); | ||
} | ||
return definition; | ||
} | ||
|
||
@Override | ||
public VariableResolverAdapter getVariableResolverAdapter() { | ||
return variableResolverAdapter; | ||
} | ||
|
||
@Override | ||
public ContentPermissionChecker contentPermissionChecker() { | ||
return contentPermissionChecker; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters