/**
 * This offers the locally stored recordings (files) to the network
 */
package org.bucksch.zeipis.store;
import org.bucksch.httpserver.*;
import org.bucksch.zeipis.all.Util;
import org.bucksch.zeipis.core.CoreServer;

public class LocalRecordingsProvider implements HTTPHandler
{
  public LocalRecordingsProvider()
  {
    CoreServer.me.dispatchingServer().registerHandler(this, "recorded/video/");
    new LocalPreviewImageProvider();
  }
  public void handle(HTTPRequest request)
    throws HTTPException
  {
    int recordingid;
    try {
      recordingid = Integer.valueOf(request.getURLRest());
    } catch (NumberFormatException e) {
      throw new HTTPException(400, request.getURLPrefix() + " must be followed by a numeric ID of the recording, not " + request.getURLRest());
    }
    StoredRecording recording;
    try {
      recording = new StoredRecording(recordingid);
    } catch (Exception e) {
      throw new HTTPException(404, "Could not find recording " + recordingid, e);
    }
    if (!recording.isLocal())
      throw new HTTPException(404, "Recording " + recordingid + " is not a file on " + Util.thisHostname());
    String descr = "file " + recording.absFilename() + " for recording id " + recordingid;

    new HTTPFileResponse().respond(request, recording.absFilename(), "video/mpeg", descr);
  }
}
