Skip to main content

新需求 取得圖片的公開網址

這次,我們有個新需求,需要多一個api,可以透過get的request來取得之前上傳的圖片的公開網址 規格如下

  /v1/temp-link/{location}/{fileName}:
get:
summary: redirect to the public link
description: redirect to the public link
tags:
- v1Asset
operationId: v1RedirectToPublicLink
parameters:
- name: location
in: path
description: save location
required: true
schema:
type: string
enum:
- "obsidian"
- "blog"
x-enum-varnames:
- OBSIDIAN
- BLOG
- name: fileName
in: path
description: save location
required: true
schema:
type: string
responses:
'302':
description: public link
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

因此,我們透過oapi-codegen產生相對應的golang的interface,我們再讓之前宣告的EchoServer去實做這次新增的api

func (e *EchoServer) V1RedirectToPublicLink(ctx echo.Context, location V1RedirectToPublicLinkParamsLocation, fileName string) error {  
//TODO implement me
panic("implement me")
}

但在這時候發現,我們開給application的fileStorer的功能只有

GetPreviewLink(file domain.CloudFile, location domain.CloudFileLocation) (link string, err error)

並沒有辦法透過string去取得public link的功能

因此,我們要另外開一個func,可以透過string來尋找link的功能

GetPreviewLinkByLocationAndFileName(location domain.CloudFileLocation, fileName string) (link string, err error)

此時意外發現,domain層內有一個可以用name產生file的func

GetCloudFileByName(name string) (file CloudFile, err error)

但此時又發現,在目前的規劃中,是沒辦法用僅用name就可以找到檔案的(因為每個檔案都會存在某一個資料夾中,所以需要一個location來存)

所以會需要將這個func多增加一個location的變數,並順手將名稱改成GetCloudFileByNameAndLocation

GetCloudFileByNameAndLocation(name string, location CloudFileLocation) (file CloudFile, err error)

中間還有一點修正,包含產生public的路徑的domain有修正,但總之整體邏輯和之前描述的差不多

最後完整的code在這邊 https://github.com/kevinyay945/2023_asset_management/tree/v0.4.0