| | 259 | |
|---|
| | 260 | |
|---|
| | 261 | security.declarePublic('newMediaObject') |
|---|
| | 262 | def newMediaObject(self, blogid, username, password, struct): |
|---|
| | 263 | """Create media object and return it's URL or exception""" |
|---|
| | 264 | |
|---|
| | 265 | self.plone_log('metaWeblog/newMediaObject') |
|---|
| | 266 | print 'metaWeblog/newMediaObject' |
|---|
| | 267 | |
|---|
| | 268 | sm = getSecurityManager() |
|---|
| | 269 | m_tool = getToolByName(self, 'portal_membership') |
|---|
| | 270 | user = m_tool.getMemberById(username) |
|---|
| | 271 | uf = getToolByName(self, 'acl_users') |
|---|
| | 272 | if not hasattr(user, 'aq_base'): |
|---|
| | 273 | user = user.__of__(uf) |
|---|
| | 274 | newSecurityManager(None, user) |
|---|
| | 275 | |
|---|
| | 276 | sbtool = getToolByName(self, 'simpleblog_tool') |
|---|
| | 277 | |
|---|
| | 278 | blog = sbtool.getByUID(blogid) |
|---|
| | 279 | |
|---|
| | 280 | media_name = struct.get('name', struct.get('name')) |
|---|
| | 281 | mime_type = struct.get('type', struct.get('type')) |
|---|
| | 282 | data = struct.get('bits', struct.get('bits')) |
|---|
| | 283 | |
|---|
| | 284 | if not 'images' in blog.objectIds(): |
|---|
| | 285 | blog.invokeFactory('BlogFolder', id = 'images', title='Container for images') |
|---|
| | 286 | images = getattr(blog, 'images') |
|---|
| | 287 | else: |
|---|
| | 288 | images = blog.images |
|---|
| | 289 | |
|---|
| | 290 | if not (mime_type.startswith('image') or mime_type.startswith('application')): |
|---|
| | 291 | return "%s - not supported mime tipe." % mime_type |
|---|
| | 292 | |
|---|
| | 293 | id = sbtool.idFromTitle(media_name) |
|---|
| | 294 | try: |
|---|
| | 295 | blog.images.invokeFactory('Image', id=id, title=media_name) #, file=data) |
|---|
| | 296 | image = getattr(blog.images, id) |
|---|
| | 297 | url = image.absolute_url() |
|---|
| | 298 | return url |
|---|
| | 299 | except Exception, e: |
|---|
| | 300 | return str(e) |
|---|