2013-09-27 17:08:19 +00:00
package gitmedia
import (
"bytes"
"io"
)
2013-10-04 19:24:02 +00:00
var MediaWarning = [ ] byte ( "#!/usr/bin/env git media smudge\n# This is a placeholder for large media, please install git-media to retrieve content\n# It is also possible you did not have the media locally, run 'git media sync' to retrieve it\n" )
2013-09-27 17:08:53 +00:00
2013-10-04 13:48:02 +00:00
func Encode ( writer io . Writer , sha string ) ( int , error ) {
written , err := writer . Write ( MediaWarning )
if err != nil {
return written , err
}
2013-09-27 17:08:19 +00:00
2013-10-04 13:48:02 +00:00
written2 , err := writer . Write ( [ ] byte ( sha ) )
return written + written2 , err
2013-09-27 17:08:19 +00:00
}
2013-10-04 13:48:02 +00:00
func Decode ( reader io . Reader ) ( string , error ) {
buf := make ( [ ] byte , 1024 )
written , err := reader . Read ( buf )
if err != nil {
return "" , err
}
2013-09-27 17:08:19 +00:00
2013-10-04 13:48:02 +00:00
return lastNonEmpty ( bytes . Split ( buf [ 0 : written ] , [ ] byte ( "\n" ) ) ) , nil
2013-09-27 17:08:19 +00:00
}
2013-10-04 13:48:02 +00:00
func lastNonEmpty ( parts [ ] [ ] byte ) string {
idx := len ( parts )
var part [ ] byte
for len ( part ) == 0 {
idx -= 1
part = parts [ idx ]
}
return string ( part )
2013-09-27 17:08:19 +00:00
}