Skip to content
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

Standardize Upload interfaces #4909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,91 +419,6 @@ public Task<HttpResponseMessage> UploadFromLocal(int portalId)
return task;
}

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public HttpResponseMessage UploadFromUrl(UploadByUrlDto dto)
{
FileUploadDto result;
WebResponse response = null;
Stream responseStream = null;
var mediaTypeFormatter = new JsonMediaTypeFormatter();
mediaTypeFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));

if (this.VerifySafeUrl(dto.Url) == false)
{
return this.Request.CreateResponse(HttpStatusCode.BadRequest);
}

try
{
var request = (HttpWebRequest)WebRequest.Create(dto.Url);
request.Credentials = CredentialCache.DefaultCredentials;
response = request.GetResponse();
responseStream = response.GetResponseStream();
if (responseStream == null)
{
throw new Exception("No server response");
}

var fileName = this.GetFileName(response);
if (string.IsNullOrEmpty(fileName))
{
fileName = HttpUtility.UrlDecode(new Uri(dto.Url).Segments.Last());
}

var portalId = dto.PortalId;
if (portalId > -1)
{
if (!this.IsPortalIdValid(portalId))
{
throw new HttpResponseException(HttpStatusCode.Unauthorized);
}
}
else
{
portalId = this.PortalSettings.PortalId;
}

result = UploadFile(responseStream, portalId, this.UserInfo, dto.Folder.ValueOrEmpty(), dto.Filter.ValueOrEmpty(),
fileName, dto.Overwrite, dto.IsHostMenu, dto.Unzip, dto.ValidationCode);

/* Response Content Type cannot be application/json
* because IE9 with iframe-transport manages the response
* as a file download
*/
return this.Request.CreateResponse(
HttpStatusCode.OK,
result,
mediaTypeFormatter,
"text/plain");
}
catch (Exception ex)
{
result = new FileUploadDto
{
Message = ex.Message,
};
return this.Request.CreateResponse(
HttpStatusCode.OK,
result,
mediaTypeFormatter,
"text/plain");
}
finally
{
if (response != null)
{
response.Close();
}

if (responseStream != null)
{
responseStream.Close();
}
}
}

private static string GetLocalizedString(string key)
{
const string resourceFile = "/App_GlobalResources/FileUpload.resx";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
$element("ul", { 'class': 'dnnButtonGroup' }).append(
$element("li").append(
$element("a", { href: "javascript:void(0);", 'class': 'upload-file active' }).text(this.options.resources.uploadFileMethod).on("click", $.proxy(this._selectUpload, this, this._uploadMethods.local))
),
$element("li").append(
$element("a", { href: "javascript:void(0);", 'class': 'from-url' }).text(this.options.resources.uploadFromWebMethod).on("click", $.proxy(this._selectUpload, this, this._uploadMethods.web))
)
),
this._$decompressOption = $element("span").append(
Expand All @@ -101,43 +98,14 @@
$element("div", { 'class': 'fu-dialog-drag-and-drop-area-message' })
)
),
$element("div", { style: 'display: none', 'class': 'fu-dialog-content-fileupload-web' }).append(
$element("table", { 'class': 'fu-dialog-url-upload-area' }).append(
$element("tbody").append(
$element("tr").append(
$element("td").append(
$element("div").append(
this._$url = $element("input", { type: 'text', title: this.options.resources.urlTooltip, placeholder: this.options.resources.urlTooltip })
.onEnter($.proxy(this._uploadByUrl, this))
.on("change keyup paste input propertychange", $.proxy(this._validateUrl, this))
.prefixInput({ prefix: "http" })
)
),
$element("td").append(
$element("a", { href: 'javascript:void(0);', 'class': 'dnnSecondaryAction' }).text(this.options.resources.uploadFromWebButtonText).on("click", $.proxy(this._uploadByUrl, this))
)
)
)
)
),
$element("div", { style: 'display: none', 'class': 'fu-fileupload-statuses-container' }).append(
$element("ul", { 'class': 'fu-fileupload-statuses' })
)
)
);

return dialog;
},

_validateUrl: function() {
var url = this._$url.val();
if (dnn.isUrl(url)) {
this._$url.removeClass("fu-dialog-url-error");
}
else {
this._$url.addClass("fu-dialog-url-error");
}
},
},

_getFileExtension: function(fileName) {
var parts = fileName.split(".");
Expand All @@ -161,55 +129,6 @@
return false;
},

_uploadByUrl: function(eventObject) {
var url = this._$url.val();
if (!dnn.isUrl(url)) {
this._$url.addClass("fu-dialog-url-error");
return;
}

if (!this._isValidExtension(url, this.options.extensions)) {
this._$url.addClass("fu-dialog-url-error");
$.dnnAlert({ title: this.options.resources.errorDialogTitle || "Error", text: this.options.resources.invalidFileExtensions });
return;
}

this._$url.removeClass("fu-dialog-url-error");
this._$url.val("");

var status = this._getInitializedStatusElement({ fileName: url }).data("status");
this._submitUrl(status);
},

_submitUrl: function (status) {
var serviceUrl = $.dnnSF().getServiceRoot("InternalServices");
var serviceSettings = {
beforeSend: $.dnnSF(this.options.moduleId).setModuleHeaders,
url: serviceUrl + "FileUpload/UploadFromUrl",
type: "POST",
async: true,
success: $.proxy(this._onUploadByUrl, this, [status.fileName]),
error: $.onAjaxError
};
serviceSettings.data = {
Url: status.fileName,
Folder: this._selectedPath(),
Overwrite: status.overwrite,
Unzip: this._extract(),
Filter: this.options.extensions.join(","),
IsHostMenu: this.options.parameters.isHostPortal,
ValidationCode: this.options.validationCode
};
$.extend(serviceSettings.data, this.options.parameters);
$.ajax(serviceSettings);
},

_onUploadByUrl: function (url, data, textStatus, jqXhr) {
this._processResponse(url[0], data);

this.$element.trigger($.Event("onfileuploadcomplete"), [data]);
},

_createFileUploadStatusElement: function (status) {
status.overwrite = false;
status.path = this._selectedPath();
Expand Down Expand Up @@ -426,12 +345,7 @@
},

_submitResource: function(status) {
if (status.data) {
this._submitFile(status);
}
else {
this._submitUrl(status);
}
this._submitFile(status);
},

_submitFile: function(status) {
Expand Down
Loading