Skip to content

Commit

Permalink
Merge pull request #94 from buddycloud/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
lloydwatkin committed Oct 21, 2014
2 parents 03c5569 + cda2a06 commit cae3124
Show file tree
Hide file tree
Showing 25 changed files with 819 additions and 380 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.project
.settings
.classpath
./mediaserver.properties
mediaserver.properties
.DS_Store
target
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
################################################################################
# Build a dockerfile for buddycloud-media-server
# Based on ubuntu
################################################################################

FROM dockerfile/java:openjdk-7-jdk

MAINTAINER Lloyd Watkin <[email protected]>

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y --no-install-recommends maven

RUN git clone https://github.com/buddycloud/buddycloud-media-server.git
RUN cd buddycloud-media-server && git checkout develop
RUN cd buddycloud-media-server && mvn package
ADD contrib/docker/start.sh /data/
RUN chmod +x start.sh
CMD ./start.sh
103 changes: 77 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,61 @@ download media from public channels - any client has access.
[xep]: http://xmpp.org/extensions/xep-0070.html
[pubsub]: https://buddycloud.org/wiki/XMPP_XEP#Follower_Management

__Build status:__ [![Build Status](https://travis-ci.org/buddycloud/buddycloud-media-server.svg?branch=master)](https://travis-ci.org/buddycloud/buddycloud-media-server)
# Build status:

Usage
-----
* Released version: [![Build Status](https://travis-ci.org/buddycloud/buddycloud-media-server.svg?branch=master)](https://travis-ci.org/buddycloud/buddycloud-media-server)
* Development version: [![Build Status](https://travis-ci.org/buddycloud/buddycloud-media-server.svg?branch=develop)](https://travis-ci.org/buddycloud/buddycloud-media-server)

#### Via the Buddycloud API
# Deployment

__Note:__ Always ensure that you have a database/XMPP server set up and running first.

## Running manually

```
mvn package
java -jar target/buddycloud-media-server-<VERSION>-jar-with-dependencies.jar
```

`mediaserver.properties` must be in the classpath.

## Docker

```
docker run -d buddycloud/media-server
```

__Note:__ Don't forget to expose the HTTP port (plus additional as required).

### Configuration

When running with docker there are two methods which you can use to configure the server. For more information see [configuration parameters](https://github.com/buddycloud/buddycloud-media-server#configuration).

#### Mounted volume configuration

'Mounted volume' configuration works the same as including a `mediaserver.properties` file. In this case you need to mount your configuration directory at `/config/media-server` on the docker image. The media server is set up to check this directory for config files.

#### Database configuration

Starting a docker container with the environment variable of `DATABASE` set to a postgresql connection string will load configuration from the database, e.g.:

```
docker run -d DATABASE="jdbc:postgresql://localhost:5432/media-server?user=media&password=tellnoone" buddycloud/media-server
```

### Datastorage

You'll also need a mount a volume to store your media files. Ensure the mounted location matches that in your configuration.

# Usage

## Via the Buddycloud API

The API endpoints are described in detail [here](https://buddycloud.org/wiki/Buddycloud_HTTP_API#.2F.3Cchannel.3E.2Fmedia.2F.3Citem.3E).

#### Using the Media Server directly
## Using the Media Server directly

##### Discovering the HTTP endpoint
### Discovering the HTTP endpoint

In order to figure out which endpoint to send HTTP calls to, we use [XMPP Service Discovery] (http://xmpp.org/extensions/xep-0030.html) against the domain running the media server. In the folowing example, we use buddycloud.org as the target domain. We first list all services provided by buddycloud.org and then we pick the one with name "Media Server".

Expand Down Expand Up @@ -85,7 +128,7 @@ The endpoint can be advertised by adding the following key (example data) to you
http.endpoint=https://api.buddycloud.org/media_proxy
```

##### If media belongs to a public channel
### If media belongs to a public channel

You don't need an ```Authorization``` header. Notice that if you're building a web frontend, embedding public media from the media-server means just creating an ```<img>``` tag.

Expand All @@ -95,17 +138,17 @@ You don't need an ```Authorization``` header. Notice that if you're building a w
curl https://demo.buddycloud.org/api/media_proxy/[email protected]/mediaId
```

##### If media belongs to a private channel
### If media belongs to a private channel

You need to verify your request via XMPP and generate an ```Authorization``` header as following:

###### Generating a transaction id
#### Generating a transaction id

As per [XEP 0070](http://www.xmpp.org/extensions/xep-0070.html), every transaction with the media server that requires authentication must have an unique identifier within the context of the client's interaction with the server. This identifier will be sent over to the media server via HTTP and then sent back to the client via XMPP in order to confirm the client's identity.

For this example, we will use **a7374jnjlalasdf82** as a transaction id.

###### Listening for confirmation
#### Listening for confirmation

Before sending the actual HTTP request, the client has to setup an XMPP listener for the confirmation request. The message sent by the media server complies with [XEP 0070](http://www.xmpp.org/extensions/xep-0070.html) and will be in the lines of:

Expand Down Expand Up @@ -159,33 +202,40 @@ curl -H "Authorization: Basic bWVkaWEtdXNlckBleGFtcGxlLmNvbS9tZWRpYS1yZXNvdXJjZT
curl -X DELETE -H "Authorization: Basic bWVkaWEtdXNlckBleGFtcGxlLmNvbS9tZWRpYS1yZXNvdXJjZTphNzM3NGpuamxhbGFzZGY4Mg==" https://demo.buddycloud.org/api/media_proxy/[email protected]/mediaId
```

Setup
-----
# Setup

The server is written on top of Java using [RESTlet](http://www.restlet.org/).

It uses [Maven](http://maven.apache.org/) to build its packages. You can build
the package manually or download it from [here](http://downloads.buddycloud.com/).

After unpacking, you can then start it by invoking:
Setup database tables using the scripts at https://raw.githubusercontent.com/buddycloud/buddycloud-media-server/master/postgres

## Configuration

### File configuration

You can configure the media server by copying `mediaserver.properties.example` to
`mediaserver.properties` in the server's root directory, and then editing as
required.

The server is able to pick up a `mediaserver.properties` file up from anywhere in the system path.

### Database configuration

Set an environment variable as per the following example:

```
mvn package
java -jar target/buddycloud-media-server-jar-with-dependencies.jar
DATABASE="jdbc:postgresql://localhost:5432/buddycloud-server?user=buddycloud&password=tellnoone"
```

The server needs to be configured to point to a Buddycloud and XMPP server.
See the *Configuration* section.
The server will then use the database values to configure itself, the configuration.properties file will be ignored.

Setup database tables using the script at https://raw.githubusercontent.com/buddycloud/buddycloud-media-server/master/resources/schema/create_schema.sql
### Configuration file

Configuration
-------------

You can configure the media server by copying `mediaserver.properties.example` to
`mediaserver.properties` in the server's root directory, and then editing as
required. This file has multiple properties definitions:
This file has multiple properties definitions:

```
# HTTP
http.port=8080
http.tests.port=9090
Expand Down Expand Up @@ -229,7 +279,9 @@ required. This file has multiple properties definitions:
# File System
media.storage.root=/tmp
media.sizelimit=1000240
```

The following configuration options are supported:

HTTP related configurations:
Expand All @@ -245,7 +297,6 @@ you **must** provide the others *https* properties.
- **http.port** (Optional): the HTTP port where the server will listen for HTTP requests (default is *8080*).
- **http.tests.port** (Optional): the HTTP port where the server will listen for HTTP requests while running tests (default is *9090*).


XMPP related:

- **xmpp.component.host** (Required): the XMPP server location where the media server's component will connect.
Expand Down
12 changes: 12 additions & 0 deletions contrib/docker/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

OPTS="-Xms1024m -Xmx1024m -XX:-OmitStackTraceInFastThrow"

export PATH=/config/media-server/:$PATH

if [ "$DATABASE" != "" ]; then
OPTS=" $OPTS -DDATABASE=\"$DATABASE\""
fi

cd buddycloud-media-server
java $OPTS -jar target/buddycloud-media-server-jar-with-dependencies.jar
5 changes: 3 additions & 2 deletions contrib/init.d/buddycloud-media-server
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ RUNFROM=/usr/share/buddycloud-media-server
DAEMON=/usr/bin/java
JAR_FILE="$(ls $RUNFROM/buddycloud-media-server-jar-with-dependencies.jar)"
JAR_FILE="$(basename $JAR_FILE)"
DAEMON_ARGS=" -Xms1024m -Xmx1024m -XX:-OmitStackTraceInFastThrow -Djava.awt.headless=true -jar $JAR_FILE"
DAEMON_ARGS=" -Xms1024m -Xmx1024m -XX:-OmitStackTraceInFastThrow -Djava.awt.headless=true \
-cp .:$JAR_FILE com.buddycloud.mediaserver.Main"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/buddycloud-media-server

Expand Down Expand Up @@ -96,4 +97,4 @@ case "$1" in
;;
esac

:
:
28 changes: 28 additions & 0 deletions postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
PostgreSQL schema
=================

Installation instructions
-------------------------

When installing the server, you must first execute `install.sql`, then all the
upgrade files in order, i.e. first `upgrade-1.sql`, then `upgrade-2.sql`, etc.:

psql -U <username> -d <db> < install.sql
psql -U <username> -d <db> < upgrade-1.sql


Upgrade instructions
--------------------

If you need to upgrade the schema version after upgrading the server software,
you'll need to be a little more careful.

First, stop the server and **back up your DB**. The simplest way to do this is
to run `pg_dump -c -U <username> <db> > backup.sql`.

Then, read the version notes below: they will tell you what you need to take
care of.

Once done you can apply the files needed for your upgrade: if your DB schema is
currently version 3 and you need version 5, you will apply `upgrade-4.sql` and
`upgrade-5.sql` but not `upgrade-3.sql` and below.
72 changes: 72 additions & 0 deletions postgres/base-hbsql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--{
-- "id": string,
-- "fileName": string,
-- "author": string,
-- "title": string,
-- "mimeType": string,
-- "description": string,
-- "uploadedDate": datetime,
-- "lastUpdatedDate": datetime,
-- "fileExtension": string,
-- "shaChecksum": string,
-- "fileSize": long,
-- "length": long,
-- "height": int,
-- "width": int
--}

CREATE TABLE medias(
id TEXT,
fileName TEXT,
entityId TEXT NOT NULL,
author TEXT NOT NULL,
title TEXT,
description TEXT,
mimeType TEXT NOT NULL,
uploadedDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
lastUpdatedDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
fileExtension TEXT,
shaChecksum TEXT NOT NULL,
fileSize BIGINT NOT NULL,
length BIGINT,
height INT,
width INT,
PRIMARY KEY(id)
);

CREATE INDEX entityUpdated_idx ON medias (lastUpdatedDate, entityId);

-- {
-- "id": string,
-- "shaChecksum": string,
-- "mediaId": string,
-- "fileSize": long,
-- "height": int,
-- "width": int
-- }

CREATE TABLE previews(
id TEXT,
shaChecksum TEXT,
mediaId TEXT REFERENCES medias(id),
fileSize BIGINT,
height INT,
width INT,
mimeType TEXT NOT NULL,
PRIMARY KEY(id)
);

CREATE TABLE avatars(
id SERIAL PRIMARY KEY,
mediaId TEXT REFERENCES medias(id),
entityId TEXT NOT NULL
);

CREATE UNIQUE INDEX entity_idx ON avatars (entityId);

-- Add a schema_version table!
CREATE TABLE schema_version (version INT NOT NULL PRIMARY KEY,
"when" TIMESTAMP,
description TEXT);
INSERT INTO schema_version (version, "when", description)
VALUES (1, NOW(), 'DB schema versioning, drop not null from fileExtension column in medias table');
1 change: 0 additions & 1 deletion resources/schema/create_schema.sql → postgres/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ CREATE TABLE avatars(
);

CREATE UNIQUE INDEX entity_idx ON avatars (entityId);

4 changes: 4 additions & 0 deletions postgres/drop-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP TABLE IF EXISTS previews CASCADE;
DROP TABLE IF EXISTS medias CASCADE;
DROP TABLE IF EXISTS avatars CASCADE;
DROP TABLE IF EXISTS schema_version CASCADE;
8 changes: 8 additions & 0 deletions postgres/upgrade-1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE medias ALTER COLUMN fileExtension DROP NOT NULL;

-- Add a schema_version table!
CREATE TABLE schema_version (version INT NOT NULL PRIMARY KEY,
"when" TIMESTAMP,
description TEXT);
INSERT INTO schema_version (version, "when", description)
VALUES (1, NOW(), 'DB schema versioning, drop not null from fileExtension column in medias table');
13 changes: 13 additions & 0 deletions postgres/upgrade-2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BEGIN TRANSACTION;

CREATE TABLE "configuration" ("key" TEXT NOT NULL,
"value" TEXT NOT NULL,
"updated" TIMESTAMP);

INSERT INTO schema_version (version, "when", description)
VALUES (8, 'now', 'Added configuration table');

COMMIT;

INSERT INTO schema_version (version, "when", description)
VALUES (2, NOW(), 'Add configuration table');
4 changes: 0 additions & 4 deletions resources/schema/drop_schema.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,12 @@ public MediaFile<File> getMedia(String userJID, String entityId, String mediaId)

String fullDirectoryPath = getDirectory(entityId);
File file = new File(fullDirectoryPath + File.separator + mediaId);

if (!file.exists()) {
Media media = dataSource.getMedia(mediaId);

if (!file.exists() || (null == media)) {
throw new MediaNotFoundException(mediaId, entityId);
}

Media media = dataSource.getMedia(mediaId);

return new MediaFile<File>(media.getMimeType(), file, media.getLastUpdatedDate());
}

Expand Down
Loading

0 comments on commit cae3124

Please sign in to comment.