github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/file/uri.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package file
     4  
     5  import (
     6  	"path/filepath"
     7  
     8  	"../convert"
     9  	"../protocol"
    10  )
    11  
    12  // URI implement protocol.URI and protocol.FileURI interface
    13  type URI struct {
    14  	uri       string
    15  	domain    string
    16  	path      string
    17  	name      string
    18  	extension string
    19  }
    20  
    21  func (uri *URI) Init(u string) {
    22  	var dir, fileName = filepath.Split(u)
    23  	uri.path = dir
    24  	uri.Rename(fileName)
    25  	// TODO::: decode more data
    26  }
    27  
    28  func (uri *URI) URI() string                  { return uri.uri }
    29  func (uri *URI) Scheme() string               { return "file" }
    30  func (uri *URI) Domain() string               { return uri.domain }
    31  func (uri *URI) Path() string                 { return uri.path }
    32  func (uri *URI) Name() string                 { return uri.name }
    33  func (uri *URI) NameWithoutExtension() string { return uri.name[:len(uri.name)-len(uri.extension)] }
    34  func (uri *URI) Extension() string            { return uri.extension }
    35  func (uri *URI) IsDirectory() bool            { return IsPathDirectory(uri.path) }
    36  func (uri *URI) PathParts() []string          { return FilePathParts(uri.path) }
    37  
    38  func IsPathDirectory(uriPath string) bool { return uriPath[len(uriPath)-1] == '/' }
    39  
    40  // Rename just rename the file name
    41  func (uri *URI) Rename(newName string) {
    42  	uri.name = newName
    43  	for i := len(newName) - 1; i >= 0; i-- {
    44  		if newName[i] == '.' {
    45  			uri.extension = newName[i+1:]
    46  		}
    47  	}
    48  }
    49  
    50  func FilePathParts(uriPath string) (parts []string) {
    51  	parts = make([]string, 0, 4)
    52  	for i := 0; i < len(uriPath); i++ {
    53  		if uriPath[i] == '/' {
    54  			parts = append(parts, uriPath[:i])
    55  			uriPath = uriPath[i+1:]
    56  			i = 0
    57  		}
    58  	}
    59  	parts = append(parts, uriPath)
    60  	return
    61  }
    62  
    63  /*
    64  ********** protocol.Codec interface **********
    65   */
    66  
    67  func (uri *URI) MediaType() string    { return "application/uri" }
    68  func (uri *URI) CompressType() string { return "" }
    69  
    70  // Marshal enecodes and return whole file data!
    71  func (uri *URI) Marshal() (data []byte) {
    72  	var ln = 5
    73  	data = make([]byte, 0, ln)
    74  	return
    75  }
    76  
    77  // MarshalTo enecodes whole file data to given data and return it with new len!
    78  func (uri *URI) MarshalTo(data []byte) []byte {
    79  	return data
    80  }
    81  
    82  // Unmarshal save given data to the file. It will overwritten exiting data.
    83  func (uri *URI) Unmarshal(data []byte) (err protocol.Error) {
    84  	var dataAsString = convert.UnsafeByteSliceToString(data)
    85  	var dir, fileName = filepath.Split(dataAsString)
    86  	uri.path = dir
    87  	uri.name = fileName
    88  	// TODO::: decode more data
    89  	return
    90  }