2
* LiveWebGalleryExtension.PhotoRequestHandler.cs
5
* Anton Keks <anton@azib.net>
7
* This is free software. See COPYING for details
19
namespace LiveWebGalleryExtension
21
public class PhotoRequestHandler : RequestHandler
23
private LiveWebGalleryStats stats;
25
public PhotoRequestHandler (LiveWebGalleryStats stats)
30
public override void Handle (string requested, Stream stream)
32
uint photo_id = uint.Parse (requested);
33
Photo photo = App.Instance.Database.Photos.Get (photo_id);
35
SendImage (photo, stream);
38
protected virtual void SendImage (Photo photo, Stream stream)
40
string path = photo.DefaultVersion.Uri.LocalPath;
41
FileInfo file_info = new FileInfo(path);
42
if (!file_info.Exists) {
43
SendError (stream, "404 The file is not on the disk");
47
FilterSet filters = new FilterSet ();
48
filters.Add (new JpegFilter ());
49
filters.Add (new ResizeFilter (1600));
51
using (FilterRequest request = new FilterRequest (photo.DefaultVersion.Uri)) {
52
filters.Convert (request);
53
file_info = new FileInfo (request.Current.LocalPath);
54
SendFile (file_info, photo, stream);
61
protected void SendFile (FileInfo file, Photo photo, Stream dest)
63
stats.BytesSent += (int)file.Length;
64
Log.DebugFormat ("Sending {0}, {1} kb", file.FullName, file.Length / 1024);
65
SendHeadersAndStartContent(dest, "Content-Type: " + MimeTypeForExt (file.Extension),
66
"Content-Length: " + file.Length,
67
"Last-Modified: " + photo.Time.ToString ("r"));
68
using (Stream src = file.OpenRead ()) {
69
byte[] buf = new byte[10240];
71
while((read = src.Read(buf, 0, buf.Length)) != 0) {
72
dest.Write (buf, 0, read);
78
public class ThumbnailRequestHandler : PhotoRequestHandler
80
public ThumbnailRequestHandler (LiveWebGalleryStats stats)
83
protected override void SendImage (Photo photo, Stream dest)
85
Gdk.Pixbuf thumb = XdgThumbnailSpec.LoadThumbnail (photo.DefaultVersion.Uri, ThumbnailSize.Large);
86
byte[] buf = thumb.SaveToBuffer ("png");
87
SendHeadersAndStartContent(dest, "Content-Type: " + MimeTypeForExt (".png"),
88
"Content-Length: " + buf.Length,
89
"Last-Modified: " + photo.Time.ToString ("r"));
90
dest.Write (buf, 0, buf.Length);