Ejemplo n.º 1
0
func (self *Converter) WriteBucketId(input bucket.Id) interface{} {
	if input == nil {
		panic("Expecting a bucket id but value is missing")
	}
	if self.TypeIdAsBinary {
		return []byte(input)
	} else {
		return encoding.Base32Encode([]byte(input))
	}
}
Ejemplo n.º 2
0
func (self *Converter) WriteKeyElement(input []byte) interface{} {
	if input == nil {
		panic("Expecting key but value is missing")
	}
	if self.KeyElementsAsBinary {
		return []byte(input)
	} else {
		return encoding.Base32Encode(input)
	}
}
Ejemplo n.º 3
0
func (self *Converter) WriteSha256Hash(input []byte) interface{} {
	if input == nil {
		panic("Expecting a hash but value is missing")
	}
	if self.HashAsBinary {
		return input
	} else {
		return encoding.Base32Encode([]byte(input))
	}
}
Ejemplo n.º 4
0
func (self *Converter) WriteSubscriptionId(
	input operations.SubscriptionId) interface{} {

	if input == nil {
		panic("Expecting a subscription ID but value is missing")
	}
	if self.SubscriptionIdAsBinary {
		return []byte(input)
	} else {
		return encoding.Base32Encode([]byte(input))
	}
}
Ejemplo n.º 5
0
func (self *incubator) incubationFilePath(state *minos.OperationState,
	id string) (fname string, ret retcode.Status) {

	clientIdAsBase32 := encoding.Base32Encode([]byte(state.Context.ClientId))
	fname, err := state.Files.Filename(self.bucketId, "_"+clientIdAsBase32, id)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	ret = retcode.NewStatusOk()
	return
}
Ejemplo n.º 6
0
func (self *public) publicFilePath(state *minos.OperationState,
	hash []byte) (fname string, ret retcode.Status) {

	hashAsBase32 := encoding.Base32Encode(hash)
	firstBytesAsDirectory := hashAsBase32[:6]
	fname, err := state.Files.Filename(self.bucketId,
		firstBytesAsDirectory, hashAsBase32)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	ret = retcode.NewStatusOk()
	return
}
Ejemplo n.º 7
0
func (self *files) Filename(bucketId bucket.Id, discrimintor string,
	name string) (
	path string, err error) {

	typeIdPath := encoding.Base32Encode([]byte(bucketId))
	subPath := pathInclude.Join(self.storagePath, typeIdPath, discrimintor)

	err = os.MkdirAll(subPath, os.ModeDir|0777)
	if err != nil {
		return
	}

	path = pathInclude.Join(subPath, name)
	return
}
Ejemplo n.º 8
0
func (self *restServer) directoryGet(ctx *restContext) {
	var status retcode.Status

	hashAsString, status := ctx.UrlParams.NonEmptyString("hash")
	if !status.IsOk() {
		ctx.WriteErrorStatus(status)
		return
	}
	hash, err := encoding.Base32Decode(hashAsString)
	if err != nil {
		status = retcode.NewStatusFmt(retcode.ErrorClient, "Hash does not seem to be a base32 hash")
		ctx.WriteErrorStatus(status)
		return
	}

	filenames := directorySubPath(ctx.UrlParams, ctx)

	curEntry := &directoryEntry{
		isDirectory: true,
		targetHash:  hash,
	}
	finalEntry, found, status := directoryGetMultiple(ctx, curEntry, filenames)
	if !found {
		status = retcode.NewStatusFmt(retcode.OkNotFound, "Entry not found")
		ctx.WriteErrorStatus(status)
		return
	}

	if finalEntry.isDirectory {
		// Output all files from directory as json
		directoryOutputAsJson(ctx, finalEntry)
	} else {
		// Redirect to blobget
		hash := finalEntry.targetHash
		hashAsString := encoding.Base32Encode(hash)
		mime := finalEntry.mimeType
		self.blobGetWithParams(ctx, hash, hashAsString, nil, mime)
	}
}
Ejemplo n.º 9
0
func (self Id) ToString() string {
	return encoding.Base32Encode([]byte(self))
}
Ejemplo n.º 10
0
func (self *Id) ToString() string {
	userIdBytes := []byte(*self)
	return encoding.Base32Encode(userIdBytes)
}
Ejemplo n.º 11
0
func (self *restServer) blobPost(ctx *restContext) {
	var status retcode.Status

	var isGlobalBlobStore bool
	bucketId, bucketIdAsString, status := ctx.ReadBucketId(&ctx.UrlParams, "bucketId", true)
	if !status.IsOk() {
		ctx.WriteErrorStatus(status)
		return
	}
	if len(bucketId) == 0 {
		// Take the global blob store if no bucket id is supplied
		bucketId = self.caprica.GlobalBlobStoreId()
		isGlobalBlobStore = true
	}

	var out interface{}

	// Begin
	beginUpload := &operations.BucketPutIn{
		BucketOperation: operations.BucketOperation{
			BucketId: bucketId,
		},
		Key:   "incubation/from_rest_api/new",
		Value: [][]byte{},
	}
	out, status = ctx.execute(beginUpload, operations.BucketPut)
	if !status.IsOk() {
		ctx.WriteErrorStatus(status)
		return
	}

	// Now put the entire payload
	for true {
		readBuffer := make([]byte, maxReadPerElement)
		numReadFromBody, err := ctx.Request.Body.Read(readBuffer)
		defer ctx.Request.Body.Close()

		if err != nil && err != io.EOF {
			status = retcode.NewStatusFmt(retcode.ErrorClient, "Could not read body from client: "+
				"%v.", err)
			ctx.WriteErrorStatus(status)
			return
		}

		if numReadFromBody > 0 {
			uploadOneElement := &operations.BucketPutIn{
				BucketOperation: operations.BucketOperation{
					BucketId: bucketId,
				},
				Key:   "incubation/from_rest_api/append",
				Value: [][]byte{readBuffer[:numReadFromBody]},
			}
			out, status = ctx.execute(uploadOneElement, operations.BucketPut)
			if !status.IsOk() {
				ctx.WriteErrorStatus(status)
				return
			}
		}
		if err == io.EOF {
			// End, have read everything
			break
		}
	}

	// Now read the hash code
	readHashCode := &operations.BucketGetIn{
		BucketOperation: operations.BucketOperation{
			BucketId: bucketId,
		},
		Key: "incubation/from_rest_api/sum",
	}
	out, status = ctx.execute(readHashCode, operations.BucketGet)
	if !status.IsOk() {
		ctx.WriteErrorStatus(status)
		return
	}
	readHashCodeRet := out.(operations.BucketGetOut)
	hashCodeValues := readHashCodeRet.Value.([][]byte)
	if len(hashCodeValues) < 2 {
		status = retcode.NewStatusFmt(retcode.ErrorServer, "Could not get calculated hash value."+
			" Blob bucket returned less than 2 value elements.")
		ctx.WriteErrorStatus(status)
		return
	}
	var hashAsBinary []byte
	err := encoding.Cbor().Decode(hashCodeValues[0], &hashAsBinary)
	if err != nil {
		status = retcode.NewStatusFmt(retcode.ErrorServer, "Cbor decoding of hash code "+
			"failed: %v.", err)
		ctx.WriteErrorStatus(status)
		return
	}
	hashAsBase32 := encoding.Base32Encode(hashAsBinary)
	var location string
	if isGlobalBlobStore {
		location = fmt.Sprintf("/data/%v", hashAsBase32)
	} else {
		location = fmt.Sprintf("/data/%v/%v", bucketIdAsString, hashAsBase32)
	}

	// And now commit that thing
	commit := &operations.BucketPutIn{
		BucketOperation: operations.BucketOperation{
			BucketId: bucketId,
		},
		Key:   "incubation/from_rest_api/finish",
		Value: [][]byte{},
	}
	out, status = ctx.execute(commit, operations.BucketPut)
	if !status.IsOk() {
		ctx.WriteErrorStatus(status)
		return
	}

	// Ok, everything ok
	ctx.WriteLocation(location)
	ctx.Writer.WriteHeader(201)
}