Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: API to save external links as attachments #6364

Merged
merged 9 commits into from
Aug 26, 2024
86 changes: 86 additions & 0 deletions api-docs/openapi/v3_0/aggregated.json
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,36 @@
]
}
},
"/apis/api.console.halo.run/v1alpha1/attachments/-/upload-from-url": {
"post": {
"operationId": "ExternalTransferAttachment",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UploadFromUrlRequest"
}
}
},
"required": true
},
"responses": {
"default": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Attachment"
}
}
},
"description": "default response"
}
},
"tags": [
"AttachmentV1alpha1Console"
]
}
},
"/apis/api.console.halo.run/v1alpha1/attachments/upload": {
"post": {
"operationId": "UploadAttachment",
Expand Down Expand Up @@ -14136,6 +14166,47 @@
]
}
},
"/apis/uc.api.content.halo.run/v1alpha1/attachments/-/upload-from-url": {
"post": {
"description": "Upload attachment from the given URL.",
"operationId": "ExternalTransferAttachment_1",
"parameters": [
{
"description": "Wait for permalink.",
"in": "query",
"name": "waitForPermalink",
"schema": {
"type": "boolean"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UploadFromUrlRequest"
}
}
},
"required": true
},
"responses": {
"default": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Attachment"
}
}
},
"description": "default response"
}
},
"tags": [
"AttachmentV1alpha1Uc"
]
}
},
"/apis/uc.api.content.halo.run/v1alpha1/posts": {
"get": {
"description": "List posts owned by the current user.",
Expand Down Expand Up @@ -22621,6 +22692,21 @@
}
}
},
"UploadFromUrlRequest": {
"required": [
"url"
],
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"url": {
"type": "string",
"format": "url"
}
}
},
"User": {
"required": [
"apiVersion",
Expand Down
52 changes: 52 additions & 0 deletions api-docs/openapi/v3_0/apis_console.api_v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@
]
}
},
"/apis/api.console.halo.run/v1alpha1/attachments/-/upload-from-url": {
"post": {
"operationId": "ExternalTransferAttachment",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UploadFromUrlRequest"
}
}
},
"required": true
},
"responses": {
"default": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Attachment"
}
}
},
"description": "default response"
}
},
"tags": [
"AttachmentV1alpha1Console"
]
}
},
"/apis/api.console.halo.run/v1alpha1/attachments/upload": {
"post": {
"operationId": "UploadAttachment",
Expand Down Expand Up @@ -6246,6 +6276,28 @@
}
}
},
"UploadFromUrlRequest": {
"required": [
"policyName",
"url"
],
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"groupName": {
"type": "string"
},
"policyName": {
"type": "string"
},
"url": {
"type": "string",
"format": "url"
}
}
},
"User": {
"required": [
"apiVersion",
Expand Down
56 changes: 56 additions & 0 deletions api-docs/openapi/v3_0/apis_uc.api_v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@
]
}
},
"/apis/uc.api.content.halo.run/v1alpha1/attachments/-/upload-from-url": {
"post": {
"description": "Upload attachment from the given URL.",
"operationId": "ExternalTransferAttachment",
"parameters": [
{
"description": "Wait for permalink.",
"in": "query",
"name": "waitForPermalink",
"schema": {
"type": "boolean"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UploadFromUrlRequest"
}
}
},
"required": true
},
"responses": {
"default": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Attachment"
}
}
},
"description": "default response"
}
},
"tags": [
"AttachmentV1alpha1Uc"
]
}
},
"/apis/uc.api.content.halo.run/v1alpha1/posts": {
"get": {
"description": "List posts owned by the current user.",
Expand Down Expand Up @@ -1943,6 +1984,21 @@
}
}
},
"UploadFromUrlRequest": {
"required": [
"url"
],
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"url": {
"type": "string",
"format": "url"
}
}
},
"UserDevice": {
"required": [
"active",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.halo.app.core.extension.service;

import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.util.function.Consumer;
import org.springframework.core.io.buffer.DataBuffer;
Expand Down Expand Up @@ -91,4 +92,15 @@ Mono<Attachment> upload(@NonNull String policyName,
*/
Mono<URI> getSharedURL(Attachment attachment, Duration ttl);

/**
* Transfer external links to attachments.
*
* @param url external url
* @param policyName policy name
* @param groupName group name
* @param filename filename
* @return attachment
*/
Mono<Attachment> uploadFromUrl(@NonNull URL url, @NonNull String policyName,
String groupName, String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
Expand Down Expand Up @@ -102,6 +104,29 @@ public RouterFunction<ServerResponse> endpoint() {
))
.response(responseBuilder().implementation(Attachment.class))
.build())
.POST("/attachments/-/upload-from-url", contentType(MediaType.APPLICATION_JSON),
request -> request.bodyToMono(UploadFromUrlRequest.class)
.flatMap(uploadFromUrlRequest -> {
var url = uploadFromUrlRequest.url();
var policyName = uploadFromUrlRequest.policyName();
var groupName = uploadFromUrlRequest.groupName();
var fileName = uploadFromUrlRequest.filename();
return attachmentService.uploadFromUrl(url, policyName,
groupName, fileName);
})
.flatMap(attachment -> ServerResponse.ok().bodyValue(attachment)),
builder -> builder
.operationId("ExternalTransferAttachment")
.tag(tag)
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(schemaBuilder().implementation(UploadFromUrlRequest.class))
))
.response(responseBuilder().implementation(Attachment.class))
.build()
)
.GET("/attachments", this::search,
builder -> {
builder
Expand Down Expand Up @@ -275,6 +300,21 @@ private boolean hasAccepts() {
}
}

public record UploadFromUrlRequest(@Schema(requiredMode = REQUIRED) URL url,
@Schema(requiredMode = REQUIRED) String policyName,
String groupName,
String filename) {
public UploadFromUrlRequest {
if (Objects.isNull(url)) {
throw new ServerWebInputException("Required url is missing.");
}

if (!StringUtils.hasText(policyName)) {
throw new ServerWebInputException("Policy name must not be blank");
}
}
}

@Schema(types = "object")
public interface IUploadRequest {

Expand Down
Loading
Loading