2016-02-08 22:29:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-12-13 05:12:39 +00:00
|
|
|
"errors"
|
2016-02-08 22:29:21 +00:00
|
|
|
"fmt"
|
2016-12-13 05:12:39 +00:00
|
|
|
"io"
|
2017-04-14 01:05:38 +00:00
|
|
|
"io/ioutil"
|
2016-02-08 22:29:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-12-13 05:12:39 +00:00
|
|
|
"strconv"
|
2016-02-08 22:29:21 +00:00
|
|
|
)
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
const fourMB = uint64(4194304)
|
|
|
|
const oneTB = uint64(1099511627776)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// File represents a file on a share.
|
2016-12-13 05:12:39 +00:00
|
|
|
type File struct {
|
2017-04-14 01:05:38 +00:00
|
|
|
fsc *FileServiceClient
|
|
|
|
Metadata map[string]string
|
|
|
|
Name string `xml:"Name"`
|
|
|
|
parent *Directory
|
|
|
|
Properties FileProperties `xml:"Properties"`
|
|
|
|
share *Share
|
|
|
|
FileCopyProperties FileCopyState
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// FileProperties contains various properties of a file.
|
2016-12-13 05:12:39 +00:00
|
|
|
type FileProperties struct {
|
2017-04-14 01:05:38 +00:00
|
|
|
CacheControl string `header:"x-ms-cache-control"`
|
|
|
|
Disposition string `header:"x-ms-content-disposition"`
|
|
|
|
Encoding string `header:"x-ms-content-encoding"`
|
|
|
|
Etag string
|
|
|
|
Language string `header:"x-ms-content-language"`
|
|
|
|
LastModified string
|
|
|
|
Length uint64 `xml:"Content-Length"`
|
|
|
|
MD5 string `header:"x-ms-content-md5"`
|
|
|
|
Type string `header:"x-ms-content-type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileCopyState contains various properties of a file copy operation.
|
|
|
|
type FileCopyState struct {
|
|
|
|
CompletionTime string
|
|
|
|
ID string `header:"x-ms-copy-id"`
|
|
|
|
Progress string
|
|
|
|
Source string
|
|
|
|
Status string `header:"x-ms-copy-status"`
|
|
|
|
StatusDesc string
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileStream contains file data returned from a call to GetFile.
|
|
|
|
type FileStream struct {
|
|
|
|
Body io.ReadCloser
|
2017-04-14 01:05:38 +00:00
|
|
|
ContentMD5 string
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// FileRequestOptions will be passed to misc file operations.
|
|
|
|
// Currently just Timeout (in seconds) but will expand.
|
|
|
|
type FileRequestOptions struct {
|
|
|
|
Timeout uint // timeout duration in seconds.
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// getParameters, construct parameters for FileRequestOptions.
|
|
|
|
// currently only timeout, but expecting to grow as functionality fills out.
|
|
|
|
func (p FileRequestOptions) getParameters() url.Values {
|
|
|
|
out := url.Values{}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
if p.Timeout != 0 {
|
|
|
|
out.Set("timeout", fmt.Sprintf("%v", p.Timeout))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileRanges contains a list of file range information for a file.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn166984.aspx
|
|
|
|
type FileRanges struct {
|
|
|
|
ContentLength uint64
|
|
|
|
LastModified string
|
|
|
|
ETag string
|
|
|
|
FileRanges []FileRange `xml:"Range"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileRange contains range information for a file.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn166984.aspx
|
|
|
|
type FileRange struct {
|
|
|
|
Start uint64 `xml:"Start"`
|
|
|
|
End uint64 `xml:"End"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr FileRange) String() string {
|
|
|
|
return fmt.Sprintf("bytes=%d-%d", fr.Start, fr.End)
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// builds the complete file path for this file object
|
|
|
|
func (f *File) buildPath() string {
|
|
|
|
return f.parent.buildPath() + "/" + f.Name
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// ClearRange releases the specified range of space in a file.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn194276.aspx
|
|
|
|
func (f *File) ClearRange(fileRange FileRange) error {
|
|
|
|
headers, err := f.modifyRange(nil, fileRange, nil)
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
2017-04-14 01:05:38 +00:00
|
|
|
return err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
return nil
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// Create creates a new file or replaces an existing one.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn194271.aspx
|
|
|
|
func (f *File) Create(maxSize uint64) error {
|
|
|
|
if maxSize > oneTB {
|
|
|
|
return fmt.Errorf("max file size is 1TB")
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
extraHeaders := map[string]string{
|
|
|
|
"x-ms-content-length": strconv.FormatUint(maxSize, 10),
|
|
|
|
"x-ms-type": "file",
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
headers, err := f.fsc.createResource(f.buildPath(), resourceFile, nil, mergeMDIntoExtraHeaders(f.Metadata, extraHeaders), []int{http.StatusCreated})
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
2017-04-14 01:05:38 +00:00
|
|
|
return err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.Properties.Length = maxSize
|
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
return nil
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// CopyFile operation copied a file/blob from the sourceURL to the path provided.
|
2016-10-28 22:46:05 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/copy-file
|
|
|
|
func (f *File) CopyFile(sourceURL string, options *FileRequestOptions) error {
|
|
|
|
extraHeaders := map[string]string{
|
|
|
|
"x-ms-type": "file",
|
|
|
|
"x-ms-copy-source": sourceURL,
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
var parameters url.Values
|
|
|
|
if options != nil {
|
|
|
|
parameters = options.getParameters()
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
headers, err := f.fsc.createResource(f.buildPath(), resourceFile, parameters, mergeMDIntoExtraHeaders(f.Metadata, extraHeaders), []int{http.StatusAccepted})
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
2017-04-14 01:05:38 +00:00
|
|
|
return err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.updateEtagLastModifiedAndCopyHeaders(headers)
|
|
|
|
return nil
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// Delete immediately removes this file from the storage account.
|
2016-02-08 22:29:21 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn689085.aspx
|
|
|
|
func (f *File) Delete() error {
|
|
|
|
return f.fsc.deleteResource(f.buildPath(), resourceFile)
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// DeleteIfExists removes this file if it exists.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn689085.aspx
|
|
|
|
func (f *File) DeleteIfExists() (bool, error) {
|
|
|
|
resp, err := f.fsc.deleteResourceNoClose(f.buildPath(), resourceFile)
|
|
|
|
if resp != nil {
|
|
|
|
defer readAndCloseBody(resp.body)
|
|
|
|
if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound {
|
|
|
|
return resp.statusCode == http.StatusAccepted, nil
|
|
|
|
}
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
return false, err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// DownloadRangeToStream operation downloads the specified range of this file with optional MD5 hash.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-file
|
|
|
|
func (f *File) DownloadRangeToStream(fileRange FileRange, getContentMD5 bool) (fs FileStream, err error) {
|
|
|
|
if getContentMD5 && isRangeTooBig(fileRange) {
|
|
|
|
return fs, fmt.Errorf("must specify a range less than or equal to 4MB when getContentMD5 is true")
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extraHeaders := map[string]string{
|
2017-04-14 01:05:38 +00:00
|
|
|
"Range": fileRange.String(),
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
if getContentMD5 == true {
|
|
|
|
extraHeaders["x-ms-range-get-content-md5"] = "true"
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
resp, err := f.fsc.getResourceNoClose(f.buildPath(), compNone, resourceFile, http.MethodGet, extraHeaders)
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
2017-04-14 01:05:38 +00:00
|
|
|
return fs, err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkRespCode(resp.statusCode, []int{http.StatusOK, http.StatusPartialContent}); err != nil {
|
|
|
|
resp.body.Close()
|
2017-04-14 01:05:38 +00:00
|
|
|
return fs, err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
fs.Body = resp.body
|
|
|
|
if getContentMD5 {
|
|
|
|
fs.ContentMD5 = resp.headers.Get("Content-MD5")
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
return fs, nil
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// Exists returns true if this file exists.
|
|
|
|
func (f *File) Exists() (bool, error) {
|
|
|
|
exists, headers, err := f.fsc.resourceExists(f.buildPath(), resourceFile)
|
|
|
|
if exists {
|
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
f.updateProperties(headers)
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
return exists, err
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// FetchAttributes updates metadata and properties for this file.
|
|
|
|
func (f *File) FetchAttributes() error {
|
|
|
|
headers, err := f.fsc.getResourceHeaders(f.buildPath(), compNone, resourceFile, http.MethodHead)
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
f.updateProperties(headers)
|
|
|
|
f.Metadata = getMetadataFromHeaders(headers)
|
|
|
|
return nil
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// returns true if the range is larger than 4MB
|
|
|
|
func isRangeTooBig(fileRange FileRange) bool {
|
|
|
|
if fileRange.End-fileRange.Start > fourMB {
|
|
|
|
return true
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
return false
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// ListRanges returns the list of valid ranges for this file.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn166984.aspx
|
|
|
|
func (f *File) ListRanges(listRange *FileRange) (*FileRanges, error) {
|
|
|
|
params := url.Values{"comp": {"rangelist"}}
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// add optional range to list
|
|
|
|
var headers map[string]string
|
|
|
|
if listRange != nil {
|
|
|
|
headers = make(map[string]string)
|
|
|
|
headers["Range"] = listRange.String()
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
resp, err := f.fsc.listContent(f.buildPath(), params, headers)
|
2016-12-13 05:12:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
defer resp.body.Close()
|
|
|
|
var cl uint64
|
|
|
|
cl, err = strconv.ParseUint(resp.headers.Get("x-ms-content-length"), 10, 64)
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
2017-04-14 01:05:38 +00:00
|
|
|
ioutil.ReadAll(resp.body)
|
2016-10-28 22:46:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
var out FileRanges
|
|
|
|
out.ContentLength = cl
|
|
|
|
out.ETag = resp.headers.Get("ETag")
|
|
|
|
out.LastModified = resp.headers.Get("Last-Modified")
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
err = xmlUnmarshal(resp.body, &out)
|
|
|
|
return &out, err
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// modifies a range of bytes in this file
|
|
|
|
func (f *File) modifyRange(bytes io.Reader, fileRange FileRange, contentMD5 *string) (http.Header, error) {
|
|
|
|
if err := f.fsc.checkForStorageEmulator(); err != nil {
|
2016-12-13 05:12:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
if fileRange.End < fileRange.Start {
|
|
|
|
return nil, errors.New("the value for rangeEnd must be greater than or equal to rangeStart")
|
|
|
|
}
|
|
|
|
if bytes != nil && isRangeTooBig(fileRange) {
|
|
|
|
return nil, errors.New("range cannot exceed 4MB in size")
|
|
|
|
}
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
uri := f.fsc.client.getEndpoint(fileServiceName, f.buildPath(), url.Values{"comp": {"range"}})
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// default to clear
|
|
|
|
write := "clear"
|
|
|
|
cl := uint64(0)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// if bytes is not nil then this is an update operation
|
|
|
|
if bytes != nil {
|
|
|
|
write = "update"
|
|
|
|
cl = (fileRange.End - fileRange.Start) + 1
|
|
|
|
}
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
extraHeaders := map[string]string{
|
|
|
|
"Content-Length": strconv.FormatUint(cl, 10),
|
|
|
|
"Range": fileRange.String(),
|
|
|
|
"x-ms-write": write,
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
if contentMD5 != nil {
|
|
|
|
extraHeaders["Content-MD5"] = *contentMD5
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
headers := mergeHeaders(f.fsc.client.getStandardHeaders(), extraHeaders)
|
|
|
|
resp, err := f.fsc.client.exec(http.MethodPut, uri, headers, bytes, f.fsc.auth)
|
|
|
|
if err != nil {
|
2016-10-28 22:46:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
defer readAndCloseBody(resp.body)
|
|
|
|
return resp.headers, checkRespCode(resp.statusCode, []int{http.StatusCreated})
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// SetMetadata replaces the metadata for this file.
|
2016-12-13 05:12:39 +00:00
|
|
|
//
|
|
|
|
// Some keys may be converted to Camel-Case before sending. All keys
|
|
|
|
// are returned in lower case by GetFileMetadata. HTTP header names
|
|
|
|
// are case-insensitive so case munging should not matter to other
|
|
|
|
// applications either.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn689097.aspx
|
2017-04-14 01:05:38 +00:00
|
|
|
func (f *File) SetMetadata() error {
|
|
|
|
headers, err := f.fsc.setResourceHeaders(f.buildPath(), compMetadata, resourceFile, mergeMDIntoExtraHeaders(f.Metadata, nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
return nil
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// SetProperties sets system properties on this file.
|
2016-10-28 22:46:05 +00:00
|
|
|
//
|
|
|
|
// Some keys may be converted to Camel-Case before sending. All keys
|
2017-04-14 01:05:38 +00:00
|
|
|
// are returned in lower case by SetFileProperties. HTTP header names
|
2016-10-28 22:46:05 +00:00
|
|
|
// are case-insensitive so case munging should not matter to other
|
|
|
|
// applications either.
|
|
|
|
//
|
2017-04-14 01:05:38 +00:00
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn166975.aspx
|
|
|
|
func (f *File) SetProperties() error {
|
|
|
|
headers, err := f.fsc.setResourceHeaders(f.buildPath(), compProperties, resourceFile, headersFromStruct(f.Properties))
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.updateEtagAndLastModified(headers)
|
|
|
|
return nil
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// updates Etag and last modified date
|
|
|
|
func (f *File) updateEtagAndLastModified(headers http.Header) {
|
|
|
|
f.Properties.Etag = headers.Get("Etag")
|
|
|
|
f.Properties.LastModified = headers.Get("Last-Modified")
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// updates Etag, last modified date and x-ms-copy-id
|
|
|
|
func (f *File) updateEtagLastModifiedAndCopyHeaders(headers http.Header) {
|
|
|
|
f.Properties.Etag = headers.Get("Etag")
|
|
|
|
f.Properties.LastModified = headers.Get("Last-Modified")
|
|
|
|
f.FileCopyProperties.ID = headers.Get("X-Ms-Copy-Id")
|
|
|
|
f.FileCopyProperties.Status = headers.Get("X-Ms-Copy-Status")
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// updates file properties from the specified HTTP header
|
|
|
|
func (f *File) updateProperties(header http.Header) {
|
|
|
|
size, err := strconv.ParseUint(header.Get("Content-Length"), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
f.Properties.Length = size
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
f.updateEtagAndLastModified(header)
|
|
|
|
f.Properties.CacheControl = header.Get("Cache-Control")
|
|
|
|
f.Properties.Disposition = header.Get("Content-Disposition")
|
|
|
|
f.Properties.Encoding = header.Get("Content-Encoding")
|
|
|
|
f.Properties.Language = header.Get("Content-Language")
|
|
|
|
f.Properties.MD5 = header.Get("Content-MD5")
|
|
|
|
f.Properties.Type = header.Get("Content-Type")
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// URL gets the canonical URL to this file.
|
|
|
|
// This method does not create a publicly accessible URL if the file
|
|
|
|
// is private and this method does not check if the file exists.
|
|
|
|
func (f *File) URL() string {
|
|
|
|
return f.fsc.client.getEndpoint(fileServiceName, f.buildPath(), url.Values{})
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// WriteRange writes a range of bytes to this file with an optional MD5 hash of the content.
|
|
|
|
// Note that the length of bytes must match (rangeEnd - rangeStart) + 1 with a maximum size of 4MB.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dn194276.aspx
|
|
|
|
func (f *File) WriteRange(bytes io.Reader, fileRange FileRange, contentMD5 *string) error {
|
|
|
|
if bytes == nil {
|
|
|
|
return errors.New("bytes cannot be nil")
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
headers, err := f.modifyRange(bytes, fileRange, contentMD5)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
|
|
|
|
f.updateEtagAndLastModified(headers)
|
2016-10-28 22:46:05 +00:00
|
|
|
return nil
|
|
|
|
}
|