-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadItem.boo
123 lines (98 loc) · 3.28 KB
/
DownloadItem.boo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* This file is a part of Flickroff
*
* Copyright (C) 2008:
*
* Authors:
* Michael Dominic K. <mdk@mdk.am>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace Flickroff
import System
import System.IO
import System.Net
import FlickrNet
class DownloadItem ():
_thumbnailUrl as string
_shortDirectory as string
static locker = Object ()
[Property (Url)]
_url as string
[Property (Filename)]
_filename as string
[Property (Directory)]
_directory as string
[Property (Extension)]
_extension as string
[Property (PhotoId)]
_photoId as string
[Property (PhotosetId)]
_photosetId as string
FullPath as string:
get:
return Path.Combine (_directory, _filename + _extension)
ShortPath as string:
get:
return Path.Combine (_shortDirectory, _filename + _extension)
def constructor (photo as Photo, photoset as Photoset, directory as string):
# FIXME No need to pre-declare vars here?
_filename = photo.Title
if photoset != null:
_directory = Path.Combine (directory, photoset.Title)
_photosetId = photoset.PhotosetId
_shortDirectory = photoset.Title
else:
_photosetId = "-1"
_directory = directory
_shortDirectory = ""
_photoId = photo.PhotoId
_url = FlickrStore.GetOriginalPhotoUrl (photo)
_extension = Path.GetExtension (_url)
_thumbnailUrl = photo.SquareThumbnailUrl
def EnsureDirectory ():
lock locker:
System.IO.Directory.CreateDirectory (_directory) if not System.IO.Directory.Exists (_directory)
def ValidateFilename ():
# Get rid of some invalid characters
for c in Path.GetInvalidFileNameChars ():
_filename = _filename.Replace (c, char ('-'))
origname = _filename
number = 2
while (Database.HasLocation (ShortPath) == true):
_filename = origname + String.Format (" ({0})", number)
number += 1
def Download ():
try:
request = WebRequest.Create (_url)
request.Timeout = 100000
inputStream = request.GetResponse ().GetResponseStream ()
outputStream = File.Create (FullPath)
buf = array (byte, 1024)
c = 0
while ((c = inputStream.Read (buf, 0, 1024)) != 0):
outputStream.Write (buf, 0, c)
inputStream.Close ()
outputStream.Close ()
except:
File.Delete (FullPath)
raise
def DownloadThumbnail () as Gdk.Pixbuf:
request = WebRequest.Create (_thumbnailUrl)
inputStream = request.GetResponse ().GetResponseStream ()
pixbuf = Gdk.Pixbuf (inputStream)
inputStream.Close ()
return pixbuf
override def ToString ():
return String.Format ("{0} => {1}", _url, FullPath)