mirror of
https://github.com/element-hq/synapse.git
synced 2024-11-23 18:15:53 +03:00
Merge pull request #2845 from matrix-org/rav/urlcache_error_handling
Handle url_previews with no content-type
This commit is contained in:
commit
9c9356512e
1 changed files with 34 additions and 21 deletions
|
@ -12,6 +12,19 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
import cgi
|
||||||
|
import datetime
|
||||||
|
import errno
|
||||||
|
import fnmatch
|
||||||
|
import itertools
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import ujson as json
|
||||||
|
import urlparse
|
||||||
|
|
||||||
from twisted.web.server import NOT_DONE_YET
|
from twisted.web.server import NOT_DONE_YET
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
@ -33,18 +46,6 @@ from synapse.http.server import (
|
||||||
from synapse.util.async import ObservableDeferred
|
from synapse.util.async import ObservableDeferred
|
||||||
from synapse.util.stringutils import is_ascii
|
from synapse.util.stringutils import is_ascii
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import fnmatch
|
|
||||||
import cgi
|
|
||||||
import ujson as json
|
|
||||||
import urlparse
|
|
||||||
import itertools
|
|
||||||
import datetime
|
|
||||||
import errno
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
import logging
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -286,17 +287,28 @@ class PreviewUrlResource(Resource):
|
||||||
url_cache=True,
|
url_cache=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
|
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
|
||||||
|
try:
|
||||||
logger.debug("Trying to get url '%s'" % url)
|
logger.debug("Trying to get url '%s'" % url)
|
||||||
length, headers, uri, code = yield self.client.get_file(
|
length, headers, uri, code = yield self.client.get_file(
|
||||||
url, output_stream=f, max_size=self.max_spider_size,
|
url, output_stream=f, max_size=self.max_spider_size,
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
# FIXME: pass through 404s and other error messages nicely
|
# FIXME: pass through 404s and other error messages nicely
|
||||||
|
logger.warn("Error downloading %s: %r", url, e)
|
||||||
|
raise SynapseError(
|
||||||
|
500, "Failed to download content: %s" % (
|
||||||
|
traceback.format_exception_only(sys.exc_type, e),
|
||||||
|
),
|
||||||
|
Codes.UNKNOWN,
|
||||||
|
)
|
||||||
yield finish()
|
yield finish()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if "Content-Type" in headers:
|
||||||
media_type = headers["Content-Type"][0]
|
media_type = headers["Content-Type"][0]
|
||||||
|
else:
|
||||||
|
media_type = "application/octet-stream"
|
||||||
time_now_ms = self.clock.time_msec()
|
time_now_ms = self.clock.time_msec()
|
||||||
|
|
||||||
content_disposition = headers.get("Content-Disposition", None)
|
content_disposition = headers.get("Content-Disposition", None)
|
||||||
|
@ -336,10 +348,11 @@ class PreviewUrlResource(Resource):
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise SynapseError(
|
logger.error("Error handling downloaded %s: %r", url, e)
|
||||||
500, ("Failed to download content: %s" % e),
|
# TODO: we really ought to delete the downloaded file in this
|
||||||
Codes.UNKNOWN
|
# case, since we won't have recorded it in the db, and will
|
||||||
)
|
# therefore not expire it.
|
||||||
|
raise
|
||||||
|
|
||||||
defer.returnValue({
|
defer.returnValue({
|
||||||
"media_type": media_type,
|
"media_type": media_type,
|
||||||
|
|
Loading…
Reference in a new issue