{"id":764,"date":"2022-01-04T16:43:53","date_gmt":"2022-01-04T07:43:53","guid":{"rendered":"https:\/\/vasyworks.yworks.net\/?page_id=764"},"modified":"2022-01-04T16:43:53","modified_gmt":"2022-01-04T07:43:53","slug":"explanation_vasyworks_media","status":"publish","type":"page","link":"https:\/\/vasyworks.yworks.net\/index.php\/explanation\/explanation_vasyworks_media\/","title":{"rendered":"Vasyworks\u3067\u306e\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306e\u8868\u793a\u306b\u3064\u3044\u3066\uff1aVasyworks\u89e3\u8aac"},"content":{"rendered":"\n<p>Vasyworks\u3067\u30ed\u30b0\u30a4\u30f3\u3092\u5fc5\u8981\u3068\u3059\u308b\u30b7\u30b9\u30c6\u30e0\u306b\u3064\u3044\u3066\u306f\u3001\u30e6\u30fc\u30b6\u304c\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306f\u76f4\u63a5\u53c2\u7167\u305b\u305a\u306b\u8868\u793a\u7528\u306e\u30d3\u30e5\u30fc\u30a2\u3092\u901a\u3057\u3066\u53c2\u7167\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002Django\u306eMEDIA\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092WEB\u516c\u958b\u305b\u305a\u306b\u30d3\u30e5\u30fc\u30a2\u3092\u901a\u3057\u3066\u8868\u793a\u3055\u305b\u308b\u3053\u3068\u3067\u3001HTML\u5185\u306b\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u30e1\u30c7\u30a3\u30a2\u53c2\u7167\u3092\u76f4\u63a5URL\u6307\u5b9a\u3057\u3066\u3082\u30ed\u30b0\u30a4\u30f3\u3057\u306a\u3044\u3068\u95b2\u89a7\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002 \u4e0b\u8a18\u306b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002 <\/p>\n\n\n\n<p>\u25a0viewer\/urls.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\nSystem Name: Vasyworks\nCopyright (C) 2020 Yasuhiro Yamamoto\n\"\"\"\nfrom django.urls import path\nfrom django.views.generic import TemplateView\nfrom .views import *\n\n\nurlpatterns = &#91;\n    path('media\/&lt;path:file_url&gt;', MediaViewerView.as_view(), name='viewer_media'),\n\n    path('', TemplateView.as_view(template_name='404.html'), name='viewer_index'),\n]\n<\/code><\/pre>\n\n\n\n<p>\u25a0viewer\/views.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\nSystem Name: Vasyworks\nCopyright (C) 2020 Yasuhiro Yamamoto\n\"\"\"\nimport os\nimport mimetypes\nfrom django.conf import settings\nfrom django.http import HttpResponse, Http404\nfrom django.views.generic import View\nfrom django.utils.decorators import method_decorator\nfrom django.contrib.auth.decorators import login_required\n\n\nclass MediaViewerView(View):\n    \"\"\"\n    \u30e1\u30c7\u30a3\u30a2\u30d3\u30e5\u30fc\u30a2\n    \"\"\"\n    @method_decorator(login_required)\n    def get(self, request, *args, **kwargs):\n        user = self.request.user\n        if not user:\n            raise Http404\n\n        file_url = kwargs.get('file_url')\n        if file_url:\n            file_path = os.path.join(settings.MEDIA_ROOT, file_url.replace('\/', os.sep))\n            file_name = self.get_file_name(file_url)\n\n            file = None\n            try:\n                file = open(file_path, 'rb')\n                content_type = self.get_content_type(file_url)\n\n                response = HttpResponse(file.read(), content_type=content_type)\n                if self.is_attachment(content_type):\n                    response&#91;'Content-Disposition'] = 'attachment; filename=\"{0}\"'.format(file_name)\n\n                file.close()\n                return response\n\n            except:\n                if file:\n                    file.close()\n                raise Http404\n\n        else:\n            raise Http404\n\n    @classmethod\n    def get_content_type(cls, url: str):\n        \"\"\" URL\u304b\u3089Content-Type\u3092\u53d6\u5f97 \"\"\"\n        ans = ''\n        if url:\n            mimetype = mimetypes.guess_type(url)\n            if mimetype:\n                ans = mimetype&#91;0]\n\n        return ans\n\n    @classmethod\n    def get_file_name(cls, url: str):\n        ans = ''\n        if url:\n            ans = url.rsplit('\/', 1)&#91;1]\n\n        return ans\n\n    @classmethod\n    def is_attachment(cls, content_type):\n        \"\"\" \u6307\u5b9a\u306eContent_Type\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5bfe\u8c61\u306a\u3089True  \"\"\"\n        ans = True\n        if 'image\/' in content_type:\n            ans = False\n        elif 'video\/' in content_type:\n            ans = False\n        elif 'application\/pdf' in content_type:\n            ans = False\n\n        return ans\n<\/code><\/pre>\n\n\n\n<p>urls.py\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u30d1\u30b9\u30b3\u30f3\u30d0\u30fc\u30bf\u306b\u300cpath\u300d\u3092\u6307\u5b9a\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\u3092\u53d7\u3051\u53d6\u308b\u3088\u3046\u306b\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\u306b\u306fDjango\u306eMEDIA\u30eb\u30fc\u30c8\uff08\/media\/\uff09\u304b\u3089\u306e\u30d1\u30b9\u3092\u533a\u5207\u308a\u6587\u5b57\u306b\u300c\/\u300d\u3092\u4f7f\u3063\u3066\u6307\u5b9a\u3057\u307e\u3059\u3002\u3061\u306a\u307f\u306bURL\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30e3\u3067\u4f7f\u3048\u308b\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30d1\u30b9\u30b3\u30f3\u30d0\u30fc\u30bf\u306fDjango3.2\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u306f\u300cstr\u300d\u300cint\u300d\u300cslug\u300d\u300cuuid\u300d\u300cpath\u300d\u306e5\u3064\u304c\u3042\u308b\u3068\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>views.py\u306eMediaViewerView\u30af\u30e9\u30b9\u306eget\u30e1\u30bd\u30c3\u30c9\u3067login_required\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u3092\u4ecb\u3055\u305a\u306b\u76f4\u63a5\u30d3\u30e5\u30fc\u30a2\u3092\u53c2\u7167\u3057\u305f\u5834\u5408\u306f\u30ed\u30b0\u30a4\u30f3\u3092\u4fc3\u3055\u308c\u307e\u3059\u3002Http\u30b5\u30fc\u30d0\u8a2d\u5b9a\u3067Django\u306emedia\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u6307\u5b9a\u3092\u884c\u308f\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u306a\u3069\u3001media\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3078\u306eURL\u76f4\u63a5\u53c2\u7167\u304c\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u304a\u3051\u3070\u3001\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u30d3\u30e5\u30fc\u30a2\u7d4c\u7531\u306b\u5236\u9650\u3067\u304d\u308b\u306e\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u3092\u5f37\u8981\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001\u3053\u306eMediaViewerView\u30af\u30e9\u30b9\u3067\u306f\u753b\u50cf\u3001\u52d5\u753b\u3001PDF\u4ee5\u5916\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u5bfe\u8c61\u306e\u5834\u5408\u306fContent-Disposition\u306battachment\u3092\u8a2d\u5b9a\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u4fc3\u3059\u3088\u3046\u306b\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vasyworks\u3067\u30ed\u30b0\u30a4\u30f3\u3092\u5fc5\u8981\u3068\u3059\u308b\u30b7\u30b9\u30c6\u30e0\u306b\u3064\u3044\u3066\u306f\u3001\u30e6\u30fc\u30b6\u304c\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306f\u76f4\u63a5\u53c2\u7167\u305b\u305a\u306b\u8868\u793a\u7528\u306e\u30d3\u30e5\u30fc\u30a2\u3092\u901a\u3057\u3066\u53c2\u7167\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002Django\u306eMEDIA\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092WEB\u516c\u958b\u305b\u305a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":567,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/764"}],"collection":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":9,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/764\/revisions"}],"predecessor-version":[{"id":777,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/764\/revisions\/777"}],"up":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/567"}],"wp:attachment":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}