Skip to content

Commit eec5d95

Browse files
committed
Yandex Disk: change file name generation logic to implement files automatic deletion
1 parent 15136b7 commit eec5d95

4 files changed

Lines changed: 28 additions & 14 deletions

File tree

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ require github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
66

77
require golang.org/x/time v0.15.0
88

9-
require github.com/google/uuid v1.6.0
10-
119
require (
1210
github.com/emersion/go-imap/v2 v2.0.0-beta.8
1311
github.com/emersion/go-message v0.18.2 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/emersion/go-message v0.18.2 h1:rl55SQdjd9oJcIoQNhubD2Acs1E6IzlZISRTK7
44
github.com/emersion/go-message v0.18.2/go.mod h1:XpJyL70LwRvq2a8rVbHXikPgKj8+aI0kGdHlg16ibYA=
55
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk=
66
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
7-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
97
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
108
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
119
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

internal/handler/listener.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,11 @@ func ListenYaDisk(ctx context.Context, cfg config.Config, vkC *api.VKClient, sto
302302
b, err := yadiskC.Download(item.Name)
303303

304304
if err == nil {
305+
s := string(b)
306+
yadiskC.UpdateNamespace(s)
307+
305308
dataMu.Lock()
306-
data = append(data, string(b))
309+
data = append(data, s)
307310
dataMu.Unlock()
308311
} else {
309312
slog.Error("yadisk: listen", "name", yadiskC.Name, "err", err)

internal/yadisk/client.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import (
1212
"path"
1313
"time"
1414

15-
"github.com/google/uuid"
15+
"github.com/nil2x/cheburnet/internal/api"
1616
"github.com/nil2x/cheburnet/internal/config"
1717
)
1818

1919
// Client is a client for interaction with Yandex Disk.
2020
type Client struct {
21-
Name string
22-
cfgAPI config.API
23-
cfgYa config.YaDisk
21+
Name string
22+
cfgAPI config.API
23+
cfgYa config.YaDisk
24+
storageC *api.StorageClient
2425
}
2526

2627
// New returns new Client for the given config.
@@ -34,9 +35,10 @@ func New(cfgAPI config.API, cfgYa config.YaDisk) *Client {
3435
}
3536

3637
c := &Client{
37-
Name: cfgYa.Name,
38-
cfgAPI: cfgAPI,
39-
cfgYa: cfgYa,
38+
Name: cfgYa.Name,
39+
cfgAPI: cfgAPI,
40+
cfgYa: cfgYa,
41+
storageC: api.NewStorageClient(),
4042
}
4143

4244
return c
@@ -102,11 +104,18 @@ func (r hrefResp) check() error {
102104

103105
// Upload uploads data in the root directory and returns name of a created file.
104106
//
107+
// The name is generated according to the internal logic. For it to work correctly
108+
// call UpdateNamespace. The name is not unique, do not use it as a constant key.
109+
//
110+
// The file is not constant and will be overwritten with future Upload calls.
111+
// File lifetime depends on Upload frequency, not expect it to be more than few minutes.
112+
// When file is overwritten, its creation time also changes.
113+
//
105114
// ext specifies file extension. Optional. Example: "txt".
106115
//
107116
// See https://yandex.ru/dev/disk-api/doc/ru/reference/upload
108117
func (c *Client) Upload(b []byte, ext string) (string, error) {
109-
name := uuid.NewString()
118+
name := c.storageC.CreateSetKey()
110119

111120
if ext != "" {
112121
name += "." + ext
@@ -116,7 +125,7 @@ func (c *Client) Upload(b []byte, ext string) (string, error) {
116125

117126
values := url.Values{}
118127
values.Set("path", path)
119-
values.Set("overwrite", "false")
128+
values.Set("overwrite", "true")
120129

121130
query := values.Encode()
122131
url := fmt.Sprintf("%v/v1/disk/resources/upload?%v", c.cfgYa.Origin, query)
@@ -256,3 +265,9 @@ func (c *Client) Items(limit int) (ItemsResp, error) {
256265

257266
return resp, nil
258267
}
268+
269+
// UpdateNamespace should be called for every Datagram value you receive from Download.
270+
// It is necessary for Upload to generate correct names.
271+
func (c *Client) UpdateNamespace(value string) {
272+
c.storageC.UpdateNamespace(value)
273+
}

0 commit comments

Comments
 (0)