Allow file fetching directly from django (dev-env)
This commit is contained in:
		
							parent
							
								
									b8434ff444
								
							
						
					
					
						commit
						b719fd5122
					
				| 
						 | 
					@ -2,7 +2,7 @@ import os
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
from base64 import b64decode
 | 
					from base64 import b64decode
 | 
				
			||||||
from django.conf import settings
 | 
					from django.conf import settings
 | 
				
			||||||
from django.http import Http404
 | 
					from django.http import FileResponse, Http404
 | 
				
			||||||
from django.views.generic import TemplateView, ListView, DetailView
 | 
					from django.views.generic import TemplateView, ListView, DetailView
 | 
				
			||||||
from django.views.generic.edit import (FormView, FormMixin, CreateView, UpdateView,
 | 
					from django.views.generic.edit import (FormView, FormMixin, CreateView, UpdateView,
 | 
				
			||||||
                                       DeleteView)
 | 
					                                       DeleteView)
 | 
				
			||||||
| 
						 | 
					@ -681,6 +681,30 @@ class MediaContent(DetailView):
 | 
				
			||||||
            'Content-Type': self.object.content_type,
 | 
					            'Content-Type': self.object.content_type,
 | 
				
			||||||
            'X-Accel-Redirect': self.object.media_file.url,
 | 
					            'X-Accel-Redirect': self.object.media_file.url,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # development direct file stream - DO NOT USE PRODUCTIVLY
 | 
				
			||||||
 | 
					        if settings.PROVIDE_FILES_DIRECTLY:
 | 
				
			||||||
 | 
					            # get media URL
 | 
				
			||||||
 | 
					            pth = self.object.media_file.url
 | 
				
			||||||
 | 
					            # remove "/media-data/"
 | 
				
			||||||
 | 
					            pth = pth.split("/media-data/",1)[1]
 | 
				
			||||||
 | 
					            # remove "/" (incase of absolute path)
 | 
				
			||||||
 | 
					            pth = pth.split(str(settings.DOWNLOAD_ROOT).lstrip("/"),1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # if we do not have a "/" at the beginning, it is not a absolute path...
 | 
				
			||||||
 | 
					            if len(pth) > 1:
 | 
				
			||||||
 | 
					                pth = pth[1]
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                pth = pth[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # build final path
 | 
				
			||||||
 | 
					            filepth = str(settings.DOWNLOAD_ROOT) + "/" + pth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # return file
 | 
				
			||||||
 | 
					            response = FileResponse(open(filepth,'rb'))
 | 
				
			||||||
 | 
					            return response
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
            return HttpResponse(headers=headers)
 | 
					            return HttpResponse(headers=headers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue