/
url_directory.go
139 lines (122 loc) · 6.01 KB
/
url_directory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package azbfs
import (
"context"
"github.com/Azure/azure-pipeline-go/pipeline"
"net/url"
"strings"
)
var directoryResourceName = "directory" // constant value for the resource query parameter
// A DirectoryURL represents a URL to the Azure Storage directory allowing you to manipulate its directories and files.
type DirectoryURL struct {
directoryClient managementClient
// filesystem is the filesystem identifier
filesystem string
// pathParameter is the file or directory path
pathParameter string
}
// NewDirectoryURL creates a DirectoryURL object using the specified URL and request policy pipeline.
func NewDirectoryURL(url url.URL, p pipeline.Pipeline) DirectoryURL {
if p == nil {
panic("p can't be nil")
}
urlParts := NewBfsURLParts(url)
directoryClient := newManagementClient(url, p)
return DirectoryURL{directoryClient: directoryClient, filesystem: urlParts.FileSystemName, pathParameter: urlParts.DirectoryOrFilePath}
}
// URL returns the URL endpoint used by the DirectoryURL object.
func (d DirectoryURL) URL() url.URL {
return d.directoryClient.URL()
}
// String returns the URL as a string.
func (d DirectoryURL) String() string {
u := d.URL()
return u.String()
}
// WithPipeline creates a new DirectoryURL object identical to the source but with the specified request policy pipeline.
func (d DirectoryURL) WithPipeline(p pipeline.Pipeline) DirectoryURL {
return NewDirectoryURL(d.URL(), p)
}
// NewFileURL creates a new FileURL object by concatenating fileName to the end of
// DirectoryURL's URL. The new FileURL uses the same request policy pipeline as the DirectoryURL.
// To change the pipeline, create the FileURL and then call its WithPipeline method passing in the
// desired pipeline object. Or, call this package's NewFileURL instead of calling this object's
// NewFileURL method.
func (d DirectoryURL) NewFileURL(fileName string) FileURL {
fileURL := appendToURLPath(d.URL(), fileName)
return NewFileURL(fileURL, d.directoryClient.Pipeline())
}
// NewDirectoryURL creates a new Directory Url for Sub directory inside the directory of given directory URL.
// The new NewDirectoryURL uses the same request policy pipeline as the DirectoryURL.
// To change the pipeline, create the NewDirectoryUrl and then call its WithPipeline method passing in the
// desired pipeline object.
func (d DirectoryURL) NewDirectoryURL(dirName string) DirectoryURL {
subDirUrl := appendToURLPath(d.URL(), dirName)
return NewDirectoryURL(subDirUrl, d.directoryClient.Pipeline())
}
// Create creates a new directory within a File System
func (d DirectoryURL) Create(ctx context.Context) (*DirectoryCreateResponse, error) {
resp, err := d.directoryClient.CreatePath(ctx, d.filesystem, d.pathParameter, &directoryResourceName, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil)
return (*DirectoryCreateResponse)(resp), err
}
// Delete removes the specified empty directory. Note that the directory must be empty before it can be deleted..
// For more information, see https://docs.microsoft.com/rest/api/storageservices/delete-directory.
func (d DirectoryURL) Delete(ctx context.Context, continuationString *string, recursive bool) (*DirectoryDeleteResponse, error) {
resp, err := d.directoryClient.DeletePath(ctx, d.filesystem, d.pathParameter, &recursive, continuationString, nil,
nil, nil, nil, nil, nil, nil, nil)
return (*DirectoryDeleteResponse)(resp), err
}
// GetProperties returns the directory's metadata and system properties.
func (d DirectoryURL) GetProperties(ctx context.Context) (*DirectoryGetPropertiesResponse, error) {
resp, err := d.directoryClient.GetPathProperties(ctx, d.filesystem, d.pathParameter, nil, nil, nil,
nil, nil, nil, nil, nil)
return (*DirectoryGetPropertiesResponse)(resp), err
}
// FileSystemURL returns the fileSystemUrl from the directoryUrl
// FileSystemURL is of the FS in which the current directory exists.
func (d DirectoryURL) FileSystemURL() FileSystemURL {
// Parse Url into FileUrlParts
// Set the DirectoryOrFilePath empty
// and generate the Url
urlParts := NewBfsURLParts(d.URL())
urlParts.DirectoryOrFilePath = ""
return NewFileSystemURL(urlParts.URL(), d.directoryClient.Pipeline())
}
// ListDirectorySegment returns files/directories inside the directory. If recursive is set to true then ListDirectorySegment will recursively
// list all files/directories inside the directory. Use an empty Marker to start enumeration from the beginning.
// After getting a segment, process it, and then call ListDirectorySegment again (passing the the previously-returned
// Marker) to get the next segment.
func (d DirectoryURL) ListDirectorySegment(ctx context.Context, marker *string, recursive bool) (*DirectoryListResponse, error) {
// Since listPath is supported on filesystem Url
// covert the directory url to fileSystemUrl
// and listPath for filesystem with directory path set in the path parameter
var maxEntriesInListOperation = int32(1000)
resp, err := d.FileSystemURL().fileSystemClient.ListPaths(ctx, recursive, d.filesystem, fileSystemResourceName, &d.pathParameter, marker,
&maxEntriesInListOperation, nil, nil, nil)
return (*DirectoryListResponse)(resp), err
}
// IsDirectory determines whether the resource at given directoryUrl is a directory Url or not
// It returns false if the directoryUrl is not able to get resource properties
// It returns false if the url represent a file in the filesystem
// TODO reconsider for SDK release
func (d DirectoryURL) IsDirectory(ctx context.Context) bool {
grep, err := d.GetProperties(ctx)
// If the error occurs while getting resource properties return false
if err != nil {
return false
}
// return false if the resource type is not
if !strings.EqualFold(grep.XMsResourceType(), directoryResourceName) {
return false
}
return true
}
// NewFileUrl converts the current directory Url into the NewFileUrl
// This api is used when the directoryUrl is to represents a file
func (d DirectoryURL) NewFileUrl() FileURL {
return NewFileURL(d.URL(), d.directoryClient.Pipeline())
}