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: add support for download resource from link #1800

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ type ResourceCreate struct {
CreatorID int `json:"-"`

// Domain specific fields
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"-"`
PublicID string `json:"publicId"`
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"-"`
PublicID string `json:"publicId"`
DownloadToLocal bool `json:"downloadToLocal"`
}

type ResourceFind struct {
Expand Down
33 changes: 30 additions & 3 deletions server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -55,9 +56,35 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}

resourceCreate.CreatorID = userID
// Only allow those external links with http prefix.
if resourceCreate.ExternalLink != "" && !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
if resourceCreate.ExternalLink != "" {
// Only allow those external links with http prefix.
if !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
}

if resourceCreate.DownloadToLocal {
resp, err := http.Get(resourceCreate.ExternalLink)
Fixed Show fixed Hide fixed
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to request "+resourceCreate.ExternalLink)
}
defer resp.Body.Close()

blob, err := io.ReadAll(resp.Body)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to read "+resourceCreate.ExternalLink)
}
resourceCreate.Blob = blob

mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to read mime from "+resourceCreate.ExternalLink)
}
resourceCreate.Type = mediaType

resourceCreate.PublicID = common.GenUUID()
resourceCreate.Filename = path.Base(resourceCreate.ExternalLink)
resourceCreate.ExternalLink = ""
}
}

resource, err := s.Store.CreateResource(ctx, resourceCreate)
Expand Down
28 changes: 26 additions & 2 deletions web/src/components/CreateResourceDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Props extends DialogProps {
onConfirm?: (resourceList: Resource[]) => void;
}

type SelectedMode = "local-file" | "external-link";
type SelectedMode = "local-file" | "external-link" | "download-link";

interface State {
selectedMode: SelectedMode;
Expand All @@ -32,6 +32,7 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
filename: "",
externalLink: "",
type: "",
downloadToLocal: false,
});
const [fileList, setFileList] = useState<File[]>([]);
const fileInputRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -70,7 +71,7 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
destroy();
};

const handleSelectedModeChanged = (mode: "local-file" | "external-link") => {
const handleSelectedModeChanged = (mode: "local-file" | "external-link" | "download-link") => {
setState((state) => {
return {
...state,
Expand Down Expand Up @@ -129,6 +130,10 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
if (resourceCreate.filename === "" || resourceCreate.externalLink === "" || resourceCreate.type === "") {
return false;
}
} else if (state.selectedMode === "download-link") {
if (resourceCreate.externalLink === "") {
return false;
}
}
return true;
};
Expand Down Expand Up @@ -161,6 +166,9 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
createdResourceList.push(resource);
}
} else {
if (state.selectedMode === "download-link") {
resourceCreate.downloadToLocal = true;
}
const resource = await resourceStore.createResource(resourceCreate);
createdResourceList.push(resource);
}
Expand Down Expand Up @@ -195,6 +203,7 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
>
<Option value="local-file">{t("resource.create-dialog.local-file.option")}</Option>
<Option value="external-link">{t("resource.create-dialog.external-link.option")}</Option>
<Option value="download-link">{t("resource.create-dialog.download-link.option")}</Option>
</Select>

{state.selectedMode === "local-file" && (
Expand Down Expand Up @@ -279,6 +288,21 @@ const CreateResourceDialog: React.FC<Props> = (props: Props) => {
</>
)}

{state.selectedMode === "download-link" && (
<>
<Typography className="!mb-1" level="body2">
{t("resource.create-dialog.external-link.link")}
</Typography>
<Input
className="mb-2"
placeholder={t("resource.create-dialog.external-link.link-placeholder")}
value={resourceCreate.externalLink}
onChange={handleExternalLinkChanged}
fullWidth
/>
</>
)}

<div className="mt-2 w-full flex flex-row justify-end items-center space-x-1">
<Button variant="plain" color="neutral" onClick={handleCloseDialog}>
{t("common.cancel")}
Expand Down
3 changes: 3 additions & 0 deletions web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
"file-name-placeholder": "File name",
"type": "Type",
"type-placeholder": "File type"
},
"download-link": {
"option": "Download link"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions web/src/types/modules/resource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface ResourceCreate {
filename: string;
externalLink: string;
type: string;
downloadToLocal: boolean;
}

interface ResourcePatch {
Expand Down