refactor most of git hawser push into hawser client.Upload()

This commit is contained in:
Rick Olson 2015-02-11 19:28:42 -07:00
parent ecc617c154
commit 6241246a97
2 changed files with 46 additions and 38 deletions

@ -133,15 +133,6 @@ func pushAsset(oid, filename string, index, totalFiles int) *hawser.WrappedError
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
}
linkMeta, status, err := hawserclient.Post(path, filename)
if err != nil && status != 302 {
return hawser.Errorf(err, "Error starting file upload %s (%s)", filename, oid)
}
if status == 200 {
return nil
}
cb, file, cbErr := hawser.CopyCallbackFile("push", filename, index, totalFiles)
if cbErr != nil {
Error(cbErr.Error())
@ -150,35 +141,11 @@ func pushAsset(oid, filename string, index, totalFiles int) *hawser.WrappedError
defer file.Close()
}
if status == 405 || status == 302 {
// Do the old style OPTIONS + PUT
status, err := hawserclient.Options(path)
if err != nil {
return hawser.Errorf(err, "Error getting options for file %s (%s)", filename, oid)
}
if status == 200 {
return nil
}
err = hawserclient.Put(path, filename, cb)
if err != nil {
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
}
return nil
} // End old style
if status != 201 {
return hawser.Errorf(err, "Unexpected HTTP response: %d for %s (%s)", status, filename, oid)
}
err = hawserclient.ExternalPut(path, filename, linkMeta, cb)
if err != nil {
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
}
return nil
return hawserclient.Upload(&hawserclient.UploadRequest{
OidPath: path,
Filename: filename,
CopyCallback: cb,
})
}
// decodeRefs pulls the sha1s out of the line read from the pre-push

@ -33,6 +33,47 @@ type link struct {
Header map[string]string `json:"header,omitempty"`
}
type UploadRequest struct {
OidPath string
Filename string
CopyCallback hawser.CopyCallback
}
func Upload(upload *UploadRequest) *hawser.WrappedError {
linkMeta, status, err := Post(upload.OidPath, upload.Filename)
if err != nil && status != 302 {
return hawser.Errorf(err, "Error starting file upload.")
}
oid := filepath.Base(upload.OidPath)
switch status {
case 200:
case 405, 302:
// Do the old style OPTIONS + PUT
status, err := Options(upload.OidPath)
if err != nil {
return hawser.Errorf(err, "Error getting options for file %s (%s)", upload.Filename, oid)
}
if status != 200 {
err = Put(upload.OidPath, upload.Filename, upload.CopyCallback)
if err != nil {
return hawser.Errorf(err, "Error uploading file %s (%s)", upload.Filename, oid)
}
}
case 201:
err = ExternalPut(upload.OidPath, upload.Filename, linkMeta, upload.CopyCallback)
if err != nil {
return hawser.Errorf(err, "Error uploading file %s (%s)", upload.Filename, oid)
}
default:
return hawser.Errorf(err, "Unexpected HTTP response: %d", status)
}
return nil
}
func Options(filehash string) (int, error) {
oid := filepath.Base(filehash)
_, err := os.Stat(filehash)