-
Notifications
You must be signed in to change notification settings - Fork 160
Store enclosure metadata without fetching remote URLs #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,15 +48,13 @@ | |
| import org.apache.roller.weblogger.ui.core.plugins.UIPluginManager; | ||
| import org.apache.roller.weblogger.ui.core.plugins.WeblogEntryEditor; | ||
| import org.apache.roller.weblogger.ui.struts2.util.UIAction; | ||
| import org.apache.roller.weblogger.util.cache.CacheManager; | ||
| import org.apache.roller.weblogger.util.EnclosureMetadata; | ||
| import org.apache.roller.weblogger.util.MailUtil; | ||
| import org.apache.roller.weblogger.util.MediacastException; | ||
| import org.apache.roller.weblogger.util.MediacastResource; | ||
| import org.apache.roller.weblogger.util.MediacastUtil; | ||
| import org.apache.roller.weblogger.util.RollerMessages; | ||
| import org.apache.roller.weblogger.util.RollerMessages.RollerMessage; | ||
| import org.apache.roller.weblogger.util.Trackback; | ||
| import org.apache.roller.weblogger.util.TrackbackNotAllowedException; | ||
| import org.apache.roller.weblogger.util.cache.CacheManager; | ||
| import org.apache.struts2.convention.annotation.AllowedMethods; | ||
| import org.apache.struts2.interceptor.validation.SkipValidation; | ||
|
|
||
|
|
@@ -192,6 +190,19 @@ public String publish() { | |
| */ | ||
| private String save() { | ||
| if (!hasActionErrors()) { | ||
| EnclosureMetadata enclosure = null; | ||
| if (!StringUtils.isEmpty(getBean().getEnclosureURL())) { | ||
| try { | ||
| enclosure = EnclosureMetadata.of( | ||
| getBean().getEnclosureURL(), | ||
| getBean().getEnclosureType(), | ||
| getBean().getEnclosureLength()); | ||
| } catch (IllegalArgumentException e) { | ||
| addError("weblogEdit.enclosureMetadataInvalid"); | ||
| return INPUT; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns before the |
||
| } | ||
| } | ||
|
|
||
| try { | ||
| WeblogEntryManager weblogEntryManager = WebloggerFactory.getWeblogger() | ||
| .getWeblogEntryManager(); | ||
|
|
@@ -223,24 +234,13 @@ private String save() { | |
| weblogEntry.setPinnedToMain(getBean().getPinnedToMain()); | ||
| } | ||
|
|
||
| if (!StringUtils.isEmpty(getBean().getEnclosureURL())) { | ||
| try { | ||
| // Fetch MediaCast resource | ||
| log.debug("Checking MediaCast attributes"); | ||
| MediacastResource mediacast = MediacastUtil | ||
| .lookupResource(getBean().getEnclosureURL()); | ||
|
|
||
| // set mediacast attributes | ||
| weblogEntry.putEntryAttribute("att_mediacast_url", | ||
| mediacast.getUrl()); | ||
| weblogEntry.putEntryAttribute("att_mediacast_type", | ||
| mediacast.getContentType()); | ||
| weblogEntry.putEntryAttribute("att_mediacast_length", "" | ||
| + mediacast.getLength()); | ||
|
|
||
| } catch (MediacastException ex) { | ||
| addMessage(getText(ex.getErrorKey())); | ||
| } | ||
| if (enclosure != null) { | ||
| weblogEntry.putEntryAttribute("att_mediacast_url", | ||
| enclosure.getUrl()); | ||
| weblogEntry.putEntryAttribute("att_mediacast_type", | ||
| enclosure.getContentType()); | ||
| weblogEntry.putEntryAttribute("att_mediacast_length", | ||
| enclosure.getLength()); | ||
| } else if ("entryEdit".equals(actionName)) { | ||
| try { | ||
| // if MediaCast string is empty, clean out MediaCast | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.roller.weblogger.util; | ||
|
|
||
| import java.util.regex.Pattern; | ||
| import org.apache.commons.validator.routines.UrlValidator; | ||
|
|
||
| /** | ||
| * Validated metadata for an RSS or Atom enclosure. | ||
| */ | ||
| public final class EnclosureMetadata { | ||
|
|
||
| private static final UrlValidator URL_VALIDATOR = new UrlValidator( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| new String[] {"http", "https"}, UrlValidator.ALLOW_LOCAL_URLS); | ||
|
|
||
| private static final Pattern MEDIA_TYPE = Pattern.compile( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This admits |
||
| "[!#$%&'*+.^_`|~0-9A-Za-z-]+/[!#$%&'*+.^_`|~0-9A-Za-z-]+"); | ||
|
|
||
| private final String url; | ||
| private final String contentType; | ||
| private final String length; | ||
|
|
||
| private EnclosureMetadata(String url, String contentType, String length) { | ||
| this.url = url; | ||
| this.contentType = contentType; | ||
| this.length = length; | ||
| } | ||
|
|
||
| public static EnclosureMetadata of(String url, String contentType, String length) { | ||
| String normalizedUrl = normalize(url); | ||
| String normalizedType = normalize(contentType); | ||
| String normalizedLength = normalize(length); | ||
|
|
||
| if (!URL_VALIDATOR.isValid(normalizedUrl)) { | ||
| throw new IllegalArgumentException("Enclosure URL must be an absolute HTTP or HTTPS URL"); | ||
| } | ||
| if (!MEDIA_TYPE.matcher(normalizedType).matches()) { | ||
| throw new IllegalArgumentException("Enclosure type must be a valid media type"); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no upper bound; |
||
|
|
||
| final long byteLength; | ||
| try { | ||
| byteLength = Long.parseLong(normalizedLength); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("Enclosure length must be a non-negative integer", e); | ||
| } | ||
| if (byteLength < 0) { | ||
| throw new IllegalArgumentException("Enclosure length must be a non-negative integer"); | ||
| } | ||
|
|
||
| return new EnclosureMetadata( | ||
| normalizedUrl, normalizedType, Long.toString(byteLength)); | ||
| } | ||
|
|
||
| private static String normalize(String value) { | ||
| return value == null ? "" : value.trim(); | ||
| } | ||
|
|
||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| public String getContentType() { | ||
| return contentType; | ||
| } | ||
|
|
||
| public String getLength() { | ||
| return length; | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fatal where the old flow was advisory (
addMessageand continue), and existing entries can't pass it: the removedMediacastUtilstoredcon.getContentType()verbatim, soatt_mediacast_typemay beaudio/mpeg; charset=utf-8orvideo/mp4;codecs=avc1, whichMEDIA_TYPErejects. The author then can't save any change to that entry until they notice and hand-edit the type. Either accept parameters in the regex (and strip them), or treat an invalid legacy value as "clear the enclosure and warn" instead of refusing the save. Also, one generic message for three fields: a blank Length (new field, previously auto-filled) produces the same text as a bad URL.