Media Resources

If you want to download, upload or delete streams on your WebService, you have to follow following steps:

1. Making the Entity streamable

Derive the entity from powerGateServer.SDK.Streamable.
If preferred you can override the ContentType and the Etag properties, in order not use the default implementations.

By setting the Content-Type the client (e.g. Chrome browser) knows how to show the file.

using System.Data.Services.Common;
using powerGateServer.SDK;

namespace HelloWorldServices.Entities
{
        [DataServiceKey("Id")]
        [DataServiceEntity]
        public class TestEntity : Streamable
        {
        	public int Id
            {
                get; set;
            }

            public TestEntity()
            {
                /**
                * Optional: setting a FileName
                * PowerGateServer adds "Content-Disposition' to response header with value: filename=value
                * e.g. The Chrome browser uses this Name if we later save the PDF in our browser
                */
                FileName = "TestFile.pdf";
            }

            public override string GetContentType()
            {
                return ContentTypes.Application.Pdf;
            }
        }
}
2. Making the ServiceMethod streamable

Derive the ServiceMethod from the generic interface IStreamableServiceMethod.
By doing this, we get additional CRUD functions for Streams: Download, Upload and DeleteStream.

using System;
using System.Collections.Generic;
using System.IO;
using HelloWorldServices.Entities;
using powerGateServer.SDK;
using FileStream = powerGateServer.SDK.Streams.FileStream;

namespace HelloWorldServices
{
        public class TestService : ServiceMethod<TestEntity>, IStreamableServiceMethod<TestEntity>
        {
            public override string Name
            {
                get { return "TestMethod"; }
            }

            public IStream Download(TestEntity entity)
            {
                return new FileStream(
                    string.Format(@"C:\ProgramData\coolOrange\powerGateServer\Plugins\HelloWorld\{0}.pdf", entity.Id));
            }

            public void Upload(TestEntity entity, IStream stream)
            {
                if (entity.Mode == TransactionMode.Update)
                    throw new NotImplementedException("Currently we just allow Create requests and no update!");

                using (var fileStream = File.Create(string.Format(@"C:\ProgramData\coolOrange\powerGateServer\Plugins\HelloWorld\{0}.pdf", entity.Id)))
                    stream.Source.CopyTo(fileStream);
            }

            public void DeleteStream(TestEntity entity)
            {
                File.Delete(string.Format(@"C:\ProgramData\coolOrange\powerGateServer\Plugins\HelloWorld\{0}.pdf", entity.Id));
            }

            public override IEnumerable<TestEntity> Query(IExpression<TestEntity> expression)
            {
                return new[]
                {
                    new TestEntity{ Id = 1},
                    new TestEntity{ Id = 2},
                    new TestEntity{ Id = 3}
                };
            }

            public override void Update(TestEntity entity)
            {
            }

            public override void Create(TestEntity entity)
            {
            }

            public override void Delete(TestEntity entity)
            {
            }
        }
}

Streaming functions

Download

Invoked when retrieving a MLE.

Syntax

IStream Download(T entity)

Parameters

Type

Name

Description

T

entity

The entity, for which the Streaming resource should be downloaded.

Return type

Returns an open and readable IStream object.

Remarks

The downloaded stream could be a FileStream oranything else.
MemoryStreams could be used too, but keep in mind that large data can couse memory problems!
A stream can be retrieved by sending this:

GET http://localhost:8080/TestBundle/TEST_SVC/TestMethod(1)/$value

Upload

Invoked when uploading a new stream to a MLE.

Syntax

void Upload(T entity, IStream stream)

Parameters

Type

Name

Description

T

entity

The entity, for which the streaming resource should be uploaded.

IStream

stream

The binary data from the request

Remarks

Get’s the MLE and an open and written IStream.
When working with large data, we consider to not read the stream-data into memory!

When performing a POST request, first the Create(T) function is called for creating the MLE itself.
The Slug-Header is used to create the MLE with the correct data.
Next the Upload function is called, for uploading the binary data.

A stream can be created by sending this request:

POST http://localhost:8080/TestBundle/TEST_SVC/TestMethod
Content-Length: '###'
Slug: Id=1

%PDF-1.4
...
%%EOF

When performing a PUT request on a MLE, the Upload-function is called with the new binary data.

For checking, if the Upload function was called for updating or creating a Stream, you can use the entity.Mode property.

A stream can be updated by sending a query with this structure:

PUT http://localhost:8080/TestBundle/TEST_SVC/TestMethod(1)
Content-Length: '###'
Slug: Id=1

%PDF-1.4

%%EOF

DeleteStream

Called when deleting the stream from a MLE.

Syntax

void DeleteStream(T entity)

Parameters

Type

Name

Description

T

entity

The entity, for which the streaming resource should be removed.

Remarks

When performing a DELETE request first the MLE becomes removed by calling the Delete-function.
After that, the remaining stream-resources will be removed by calling the DeleteStream function.