Ejemplo n.º 1
0
    def downloading(self,author,image):
        from django.core.files.temp import NamedTemporaryFile
        from moto.moe.image import ImagePost
        from moto.moe.api.serializers import UserSerializer
        import os
        # file_temp = NamedTemporaryFile(delete=True)
        chunk_size = 1024
        print('downloading:', image.url)
        headers = {
              'Referer':self.rooturl,
              'User-Agent':self.ua
                }
        self.todo.remove(image.url)
        self.busy.add(image.url)
        try:
            r = yield from aiohttp.request(
                'get', image.url, connector=self.connector,headers=headers,cookies=requests.utils.dict_from_cookiejar(self.cj))
        except Exception as exc:
            print('...', image.url, 'has error', repr(str(exc)))
            self.done[image.url] = False
        else:
            with NamedTemporaryFile(delete=True) as fd:
                 while True:
                    chunk = yield from r.content.read(chunk_size)
                    if not chunk:
                        break
                    fd.write(chunk)
                 # fd
                 # a = urlparse(image.url)
                 # filename = os.path.basename(a.path)
                 image_post = ImagePost(author)
                 image_post.save(fd)
                 data = {'author':UserSerializer(author).data, 'image':image_post.image,'source':self.rooturl}
                 self.results.append(data)
                 print('image file name:', image_post.image)

            r.close()
            self.done[image.url] = True

        self.busy.remove(image.url)
        print(len(self.done), 'completed tasks,', len(self.tasks),
              'still pending, todo', len(self.todo))
Ejemplo n.º 2
0
 def create(self,request,*args,**kwargs):
     chunk = request.FILES.get('file')
     r = ResumableFile( request.POST)
     if r.chunk_exists:
         r.delete_chunks()
         return Response('chunk already exists')
     r.process_chunk(chunk)
     if r.is_complete:
         image_post = ImagePost(request.user)
         image_post.save(r)
         r.delete_chunks()
         data={'author':UserSerializer(request.user).data, 'image':image_post.image}
         serializer = self.get_serializer(data=data)
         serializer.is_valid(raise_exception=True)
         self.perform_create(serializer)
         headers = self.get_success_headers(serializer.data)
         request.session['post_id'] = serializer.data['id']
         return Response(serializer.data,  headers=headers)
     else:
         return Response()