@@ -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.
2020type 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
108117func (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