@@ -20,57 +20,56 @@ func (h *Handler) ConvertImage(ctx context.Context, portal *bridgev2.Portal, int
2020 }
2121
2222 client := h .NewClient ()
23- oid := data .ContentMetadata ["OID" ]
24- isPlainMedia := oid == ""
25-
26- // For plain media, the image is stored at r/talk/m/{messageID}
27- if isPlainMedia {
28- oid = data .ID
29- }
30-
31- if oid == "" {
23+ downloadSource := lineImageDownloadSource (data )
24+ if downloadSource .publicPath == "" && downloadSource .oid == "" {
3225 return nil , nil
3326 }
3427
3528 mediaCategory := lineMediaCategory (data .ContentMetadata )
36- downloadOptions := lineOBSDownloadOptions (data .ContentMetadata , isPlainMedia )
37- talkMetaMessageID := obsTalkMetaMessageID (data .ID , isPlainMedia )
29+ downloadOptions := lineOBSDownloadOptions (data .ContentMetadata , downloadSource . isPlainMedia )
30+ talkMetaMessageID := obsTalkMetaMessageID (data .ID , downloadSource . isPlainMedia )
3831
3932 var imgData []byte
4033 var err error
4134 dlStart := time .Now ()
4235 h .Log .Debug ().
43- Str ("oid" , oid ).
36+ Str ("oid" , downloadSource . oid ).
4437 Str ("msg_id" , data .ID ).
4538 Str ("tid" , downloadOptions .TID ).
4639 Str ("media_category" , mediaCategory ).
4740 Bool ("has_obs_pop" , downloadOptions .OBSPop != "" ).
48- Bool ("plain_media" , isPlainMedia ).
41+ Bool ("plain_media" , downloadSource .isPlainMedia ).
42+ Bool ("public_resource" , downloadSource .publicPath != "" ).
4943 Msg ("Downloading image from LINE OBS" )
50- if isPlainMedia {
51- imgData , err = client .DownloadOBSWithSIDOptions (ctx , oid , talkMetaMessageID , "m" , downloadOptions )
44+ if downloadSource .publicPath != "" {
45+ imgData , err = client .DownloadOBSPublicResource (ctx , downloadSource .publicPath )
46+ } else if downloadSource .isPlainMedia {
47+ imgData , err = client .DownloadOBSWithSIDOptions (ctx , downloadSource .oid , talkMetaMessageID , "m" , downloadOptions )
5248 } else {
53- imgData , err = client .DownloadOBSWithOptions (ctx , oid , talkMetaMessageID , downloadOptions )
49+ imgData , err = client .DownloadOBSWithOptions (ctx , downloadSource . oid , talkMetaMessageID , downloadOptions )
5450 }
5551
5652 // Refresh token if we get a 401
57- if newClient , ok := h .tryRecoverClient (ctx , client , err ); ok {
58- client = newClient
59- if isPlainMedia {
60- imgData , err = client .DownloadOBSWithSIDOptions (ctx , oid , talkMetaMessageID , "m" , downloadOptions )
61- } else {
62- imgData , err = client .DownloadOBSWithOptions (ctx , oid , talkMetaMessageID , downloadOptions )
53+ if downloadSource .publicPath == "" {
54+ if newClient , ok := h .tryRecoverClient (ctx , client , err ); ok {
55+ client = newClient
56+ if downloadSource .isPlainMedia {
57+ imgData , err = client .DownloadOBSWithSIDOptions (ctx , downloadSource .oid , talkMetaMessageID , "m" , downloadOptions )
58+ } else {
59+ imgData , err = client .DownloadOBSWithOptions (ctx , downloadSource .oid , talkMetaMessageID , downloadOptions )
60+ }
6361 }
62+ h .handleFinalAuthError (ctx , client , err )
6463 }
65- h .handleFinalAuthError (ctx , client , err )
6664 downloadDuration := time .Since (dlStart )
6765
6866 if err != nil {
6967 h .Log .Warn ().
7068 Err (err ).
71- Str ("oid" , oid ).
69+ Str ("oid" , downloadSource . oid ).
7270 Str ("msg_id" , data .ID ).
73- Bool ("plain_media" , isPlainMedia ).
71+ Bool ("plain_media" , downloadSource .isPlainMedia ).
72+ Bool ("public_resource" , downloadSource .publicPath != "" ).
7473 Dur ("download_duration" , downloadDuration ).
7574 Msg ("Failed to download image from OBS" )
7675 return mediaDownloadFailure ("Image" , err , relatesTo )
@@ -147,6 +146,26 @@ func (h *Handler) ConvertImage(ctx context.Context, portal *bridgev2.Portal, int
147146 }, nil
148147}
149148
149+ type imageDownloadSource struct {
150+ publicPath string
151+ oid string
152+ isPlainMedia bool
153+ }
154+
155+ func lineImageDownloadSource (data line.Message ) imageDownloadSource {
156+ if publicPath := data .ContentMetadata ["DOWNLOAD_URL" ]; publicPath != "" {
157+ return imageDownloadSource {publicPath : publicPath }
158+ }
159+
160+ oid := data .ContentMetadata ["OID" ]
161+ if oid != "" {
162+ return imageDownloadSource {oid : oid }
163+ }
164+
165+ // For plain media, the image is stored at r/talk/m/{messageID}.
166+ return imageDownloadSource {oid : data .ID , isPlainMedia : true }
167+ }
168+
150169func lineMediaCategory (metadata map [string ]string ) string {
151170 if metadata == nil || metadata ["MEDIA_CONTENT_INFO" ] == "" {
152171 return ""
0 commit comments