Changeset 4314
- Timestamp:
- 09/22/08 04:48:38 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
screenshotsplugin/0.11/tracscreenshots/api.py
r4304 r4314 244 244 self._edit_item(context, 'screenshot', id, tmp_screenshot) 245 245 246 247 246 # Delete item functions 248 247 screenshotsplugin/0.11/tracscreenshots/core.py
r4307 r4314 179 179 screenshot = api.get_screenshot(context, screenshot_id) 180 180 181 if screenshot: 182 # Set missing dimensions. 183 width = width or screenshot['width'] 184 height = height or screenshot['height'] 185 186 if format == 'html': 187 # Format screenshot for presentation. 188 screenshot['author'] = format_to_oneliner(self.env, context, 189 screenshot['author']) 190 screenshot['name'] = format_to_oneliner(self.env, context, 191 screenshot['name']) 192 screenshot['description'] = format_to_oneliner(self.env, 193 context, screenshot['description']) 194 screenshot['time'] = pretty_timedelta(to_datetime( 195 screenshot['time'], utc)) 196 197 # For HTML preview format return template. 198 self.data['screenshot'] = screenshot 199 return ('screenshot', None) 181 # Check if requested screenshot exists. 182 if not screenshot: 183 if context.req.perm.has_permission('SCREENSHOTS_ADMIN'): 184 context.req.redirect(context.req.href.screenshots( 185 action = 'add')) 200 186 else: 201 # Prepare screenshot filename. 202 name, ext = os.path.splitext(screenshot['file']) 203 format = (format == 'raw') and ext or '.' + format 204 path = os.path.join(self.path, to_unicode( 205 screenshot['id'])) 206 filename = os.path.join(path, '%s-%sx%s%s' % (name, 207 width, height, format)) 208 orig_name = os.path.join(path, '%s-%sx%s%s' % (name, 209 screenshot['width'], screenshot['height'], ext)) 210 self.log.debug('filemame: %s' % (filename,)) 211 212 # Create requested file from original if not exists. 213 if not os.path.exists(filename): 214 self._create_image(orig_name, path, name, format, 215 width, height) 216 217 # Send file to request. 218 context.req.send_header('Content-Disposition', 219 'attachment;filename=%s' % (os.path.basename( 220 filename))) 221 context.req.send_header('Content-Description', 222 screenshot['description']) 223 context.req.send_file(filename, mimetypes.guess_type(filename) 224 [0]) 187 raise TracError('Screenshot not found.') 188 189 # Set missing dimensions. 190 width = width or screenshot['width'] 191 height = height or screenshot['height'] 192 193 if format == 'html': 194 # Format screenshot for presentation. 195 screenshot['author'] = format_to_oneliner(self.env, context, 196 screenshot['author']) 197 screenshot['name'] = format_to_oneliner(self.env, context, 198 screenshot['name']) 199 screenshot['description'] = format_to_oneliner(self.env, 200 context, screenshot['description']) 201 screenshot['time'] = pretty_timedelta(to_datetime( 202 screenshot['time'], utc)) 203 204 # For HTML preview format return template. 205 self.data['screenshot'] = screenshot 206 return ('screenshot', None) 225 207 else: 226 raise TracError('Screenshot not found.') 208 # Prepare screenshot filename. 209 name, ext = os.path.splitext(screenshot['file']) 210 format = (format == 'raw') and ext or '.' + format 211 path = os.path.join(self.path, to_unicode( 212 screenshot['id'])) 213 filename = os.path.join(path, '%s-%sx%s%s' % (name, 214 width, height, format)) 215 orig_name = os.path.join(path, '%s-%sx%s%s' % (name, 216 screenshot['width'], screenshot['height'], ext)) 217 218 self.log.debug('filemame: %s' % (filename,)) 219 220 # Create requested file from original if not exists. 221 if not os.path.exists(filename): 222 self._create_image(orig_name, path, name, format, 223 width, height) 224 225 # Send file to request. 226 context.req.send_header('Content-Disposition', 227 'attachment;filename=%s' % (os.path.basename( 228 filename))) 229 context.req.send_header('Content-Description', 230 screenshot['description']) 231 context.req.send_file(filename, mimetypes.guess_type(filename) 232 [0]) 227 233 228 234 elif action == 'add': … … 247 253 name, ext = os.path.splitext(filename) 248 254 filename = name + ext.lower() 249 250 # Check correct file type.251 reg = re.compile(r'^(.*)[.](.*)$')252 result = reg.match(filename)253 if result:254 if not result.group(2).lower() in self.ext:255 raise TracError('Unsupported uploaded file type.')256 else:257 raise TracError('Unsupported uploaded file type.')258 255 259 256 # Create image object. … … 300 297 301 298 # Prepare file paths 299 name, ext = os.path.splitext(screenshot['file']) 302 300 path = os.path.join(self.path, unicode(screenshot['id'])) 303 filepath = os.path.join(path, '%s-%ix%i .%s' % (result.group(1),304 screenshot['width'], screenshot['height'], result.group(2)))301 filepath = os.path.join(path, '%s-%ix%i%s' % (name, 302 screenshot['width'], screenshot['height'], ext)) 305 303 path = os.path.normpath(path) 306 304 filepath = os.path.normpath(filepath) 305 307 306 self.log.debug('path: %s' % (path,)) 308 307 self.log.debug('filename: %s' % (filepath,)) … … 317 316 except Exception, error: 318 317 api.delete_screenshot(context, screenshot['id']) 319 try:320 self.log.debug(error)321 except:322 pass323 318 try: 324 319 os.remove(filename) … … 366 361 'Screenshot not found.') 367 362 363 # Get image file from request. 364 image = context.req.args['image'] 365 if hasattr(image, 'filename') and image.filename: 366 in_file, filename = self._get_file_from_req(context.req) 367 name, ext = os.path.splitext(filename) 368 filename = name + ext.lower() 369 else: 370 filename = None 371 368 372 # Construct screenshot dictionary from form values. 369 373 screenshot = {'name' : context.req.args.get('name'), … … 374 378 'components' : context.req.args.get( 375 379 'components') or [], 376 'versions' : context.req.args.get('versions') or []} 380 'versions' : context.req.args.get('versions') or \ 381 []} 382 383 # Update dimensions and filename if image file is updated. 384 if filename: 385 image = Image.open(in_file) 386 screenshot['file'] = filename 387 screenshot['width'] = image.size[0] 388 screenshot['height'] = image.size[1] 377 389 378 390 # Convert components and versions to list if only one item is … … 383 395 screenshot['versions'] = [screenshot['versions']] 384 396 397 self.log.debug(screenshot) 398 385 399 # Edit screenshot. 386 400 api.edit_screenshot(context, screenshot_id, screenshot) 387 401 402 # Prepare file paths. 403 if filename: 404 name, ext = os.path.splitext(screenshot['file']) 405 path = os.path.join(self.path, unicode(screenshot_id)) 406 filepath = os.path.join(path, '%s-%ix%i%s' % (name, 407 screenshot['width'], screenshot['height'], ext)) 408 path = os.path.normpath(path) 409 filepath = os.path.normpath(filepath) 410 411 self.log.debug('path: %s' % (path,)) 412 self.log.debug('filepath: %s' % (filepath,)) 413 414 # Delete present images. 415 try: 416 for file in os.listdir(path): 417 file = os.path.join(path, file) 418 file = os.path.normpath(file) 419 os.remove(file) 420 except Exception, error: 421 raise TracError('Error deleting screenshot. Original' \ 422 ' message was: %s' % (error,)) 423 424 # Store uploaded image. 425 try: 426 out_file = open(filepath, 'wb+') 427 in_file.seek(0) 428 shutil.copyfileobj(in_file, out_file) 429 out_file.close() 430 except Exception, error: 431 try: 432 os.remove(filename) 433 except: 434 pass 435 raise TracError('Error storing file. Is directory' \ 436 ' specified in path config option in [screenshots]' \ 437 ' section of trac.ini existing? Original message was: %s' \ 438 % (error,)) 388 439 389 440 # Notify change listeners. … … 392 443 old_screenshot) 393 444 394 # Clear idto prevent display of edit and delete button.445 # Clear ID to prevent display of edit and delete button. 395 446 context.req.args['id'] = None 396 447 … … 408 459 raise TracError('Deleted screenshot not found.', 409 460 'Screenshot not found.') 461 462 # Delete screenshot. 463 api.delete_screenshot(context, screenshot['id']) 464 465 #Â Delete screenshot files. Don't append any other files there :-). 466 path = os.path.join(self.path, to_unicode(screenshot['id'])) 467 path = os.path.normpath(path) 468 469 self.log.debug('path: %s' % (path,)) 470 410 471 try: 411 # Delete screenshot.412 api.delete_screenshot(context, screenshot['id'])413 414 #Â Delete screenshot files. Don't append any other files there :-).415 path = os.path.join(self.path, to_unicode(screenshot['id']))416 path = os.path.normpath(path)417 self.log.debug('path: %s' % (path,))418 472 for file in os.listdir(path): 419 473 file = os.path.join(path, file) … … 527 581 raise TracError('Can\'t upload empty file.') 528 582 filename = os.path.basename(image.filename).decode('utf-8') 529 self.log.debug(filename) 583 584 # Check correct file type. 585 reg = re.compile(r'^(.*)[.](.*)$') 586 result = reg.match(filename) 587 if result: 588 if not result.group(2).lower() in self.ext: 589 raise TracError('Unsupported uploaded file type.') 590 else: 591 raise TracError('Unsupported uploaded file type.') 592 530 593 return image.file, filename 531 594 screenshotsplugin/0.11/tracscreenshots/templates/screenshot-add.html
r3271 r4314 25 25 <input type="text" id="description" name="description" value="${(req.args.action == 'edit') and screenshots.screenshot.description or ''}"/><br/> 26 26 </div> 27 <div py:if="req.args.action == 'add'"class="field">27 <div class="field"> 28 28 <label for="image">Image File:</label><br/> 29 29 <input type="file" id="image" name="image" value=""/><br/>
