private HttpResponseMessage GetPackageFile(Guid packageId, System.Version currUmbracoVersion, bool includeHidden) { var pckRepoService = new PackageRepositoryService(Umbraco, Members, DatabaseContext); var details = pckRepoService.GetDetails(packageId, currUmbracoVersion, includeHidden); if (details == null) { throw new InvalidOperationException("No package found with id " + packageId); } if (details.ZipUrl.IsNullOrWhiteSpace()) { throw new InvalidOperationException("This package is not compatible with the Umbraco version " + currUmbracoVersion); } var wf = new WikiFile(details.ZipFileId); if (wf == null) { throw new InvalidOperationException("Could not find wiki file by id " + details.ZipFileId); } wf.UpdateDownloadCounter(true, true); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(wf.ToByteArray()) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return(result); }
internal static WikiFile PackageFileByGuid(Guid pack) { XPathNodeIterator xpn = umbraco.library.GetXmlNodeByXPath("descendant::* [@isDoc and translate(packageGuid,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = translate('" + pack.ToString() + "','ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]"); if (xpn.MoveNext()) { if (xpn.Current is IHasXmlNode) { Node node = new Node(((IHasXmlNode)xpn.Current).GetNode()); string fileId = safeProperty(node, "file"); int _id; if (int.TryParse(fileId, out _id)) { string cookieName = "ProjectFileDownload" + fileId; //we clear the cookie on the server just to be sure the download is accounted for if (HttpContext.Current.Request.Cookies[cookieName] != null) { HttpCookie myCookie = new HttpCookie(cookieName); myCookie.Expires = DateTime.Now.AddDays(-1d); HttpContext.Current.Response.Cookies.Add(myCookie); } WikiFile wf = new WikiFile(_id); wf.UpdateDownloadCounter(true, true); return(wf); } } } return(null); }
public byte[] GetPackageFile(string packageGuid, string umbracoVersion) { var umbHelper = new UmbracoHelper(UmbracoContext.Current); var pckRepoService = new PackageRepositoryService(umbHelper, umbHelper.MembershipHelper, ApplicationContext.Current.DatabaseContext); System.Version currUmbracoVersion; if (!System.Version.TryParse(umbracoVersion, out currUmbracoVersion)) { throw new InvalidOperationException("Could not parse the version specified " + umbracoVersion); } var guid = new Guid(packageGuid); var details = pckRepoService.GetDetails(guid, currUmbracoVersion, true); if (details == null) { throw new InvalidOperationException("No package found with id " + packageGuid); } if (details.ZipUrl.IsNullOrWhiteSpace()) { throw new InvalidOperationException("This package is not compatible with the Umbraco version " + umbracoVersion); } var wf = new WikiFile(details.ZipFileId); if (wf == null) { throw new InvalidOperationException("Could not find wiki file by id " + details.ZipFileId); } wf.UpdateDownloadCounter(true, true); return(wf.ToByteArray()); }
public byte[] fetchPackage(string packageGuid) { var umbHelper = new UmbracoHelper(UmbracoContext.Current); var pckRepoService = new PackageRepositoryService(umbHelper, umbHelper.MembershipHelper, ApplicationContext.Current.DatabaseContext); //This doesn't matter what we set it to so long as it's below 7.5 since that is the version we introduce strict dependencies var currUmbracoVersion = new System.Version(4, 0, 0); var guid = new Guid(packageGuid); var details = pckRepoService.GetDetails(guid, currUmbracoVersion, true); if (details == null) { throw new InvalidOperationException("No package found with id " + packageGuid); } if (details.ZipUrl.IsNullOrWhiteSpace()) { throw new InvalidOperationException("This package is not compatible with your Umbraco version"); } var wf = new WikiFile(details.ZipFileId); if (wf == null) { throw new InvalidOperationException("Could not find wiki file by id " + details.ZipFileId); } wf.UpdateDownloadCounter(true, true); return(wf.ToByteArray()); }
protected void Page_Load(object sender, EventArgs e) { if (Request["id"] == null) { return; } var fileId = int.Parse(Request["id"]); var wikiFile = new WikiFile(fileId); wikiFile.UpdateDownloadCounter(false, wikiFile.FileType == "package"); using (var sqlHelper = BL.Application.SqlHelper) { var path = sqlHelper.ExecuteScalar <string>( "Select path from wikiFiles where id = @id;", sqlHelper.CreateParameter("@id", fileId)); var file = sqlHelper.ExecuteScalar <string>( "Select name from wikiFiles where id = @id;", sqlHelper.CreateParameter("@id", fileId)); var fileinfo = new System.IO.FileInfo(Server.MapPath(path)); var extension = System.IO.Path.GetExtension(Server.MapPath(path)); var type = ""; // set known types based on file extension if (extension != null) { switch (extension.ToLower()) { case ".tif": case ".tiff": type = "image/tiff"; break; case ".jpg": case ".jpeg": type = "image/jpeg"; break; case ".gif": type = "image/gif"; break; case ".docx": case ".doc": case ".rtf": type = "Application/msword"; break; case ".pdf": type = "Application/pdf"; break; case ".png": type = "image/png"; break; case ".bmp": type = "image/bmp"; break; default: type = "application/octet-stream"; break; } } Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename= " + MakeSafeFileName(file)); Response.AddHeader("Content-Length", fileinfo.Length.ToString()); Response.ContentType = type; Response.WriteFile(path); } }
protected void Page_Load(object sender, EventArgs e) { if (Request["id"] != null) { int fileId = int.Parse(Request["id"]); WikiFile wf = new WikiFile(fileId); wf.UpdateDownloadCounter(false, Request["release"] != null); // HttpCookie cookie = HttpContext.Current.Request.Cookies["ProjectFileDownload" + fileId]; // if (cookie == null) // { // int downloads = 0; // downloads = BL.Application.SqlHelper.ExecuteScalar<int>( // "Select downloads from wikiFiles where id = @id;", // BL.Application.SqlHelper.CreateParameter("@id", fileId)); // downloads = downloads + 1; // BL.Application.SqlHelper.ExecuteNonQuery( // "update wikiFiles set downloads = @downloads where id = @id;", // BL.Application.SqlHelper.CreateParameter("@id", fileId), // BL.Application.SqlHelper.CreateParameter("@downloads", downloads)); // int _currentMember = 0; // Member m = Member.GetCurrentMember(); // if(m != null) // _currentMember = m.Id; // WikiFile wf = new WikiFile(fileId); // if (Request["release"] != null) // { // BL.Application.SqlHelper.ExecuteNonQuery( // @"insert into projectDownload(projectId,memberId,timestamp) // values((select nodeId from wikiFiles where id = @id) ,@memberId, getdate())", // BL.Application.SqlHelper.CreateParameter("@id", fileId), // BL.Application.SqlHelper.CreateParameter("@memberId", _currentMember)); // } // cookie = new HttpCookie("ProjectFileDownload" + fileId); // cookie.Expires = DateTime.Now.AddHours(1); // HttpContext.Current.Response.Cookies.Add(cookie); // } string path = BL.Application.SqlHelper.ExecuteScalar <string>( "Select path from wikiFiles where id = @id;", BL.Application.SqlHelper.CreateParameter("@id", fileId)); string file = BL.Application.SqlHelper.ExecuteScalar <string>( "Select name from wikiFiles where id = @id;", BL.Application.SqlHelper.CreateParameter("@id", fileId)); System.IO.FileInfo fileinfo = new System.IO.FileInfo(Server.MapPath(path)); string extension = System.IO.Path.GetExtension(Server.MapPath(path)); string type = ""; // set known types based on file extension if (extension != null) { switch (extension.ToLower()) { case ".tif": case ".tiff": type = "image/tiff"; break; case ".jpg": case ".jpeg": type = "image/jpeg"; break; case ".gif": type = "image/gif"; break; case ".docx": case ".doc": case ".rtf": type = "Application/msword"; break; case ".pdf": type = "Application/pdf"; break; case ".png": type = "image/png"; break; case ".bmp": type = "image/bmp"; break; default: type = "application/octet-stream"; break; } } Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename= " + MakeSafeFileName(file)); Response.AddHeader("Content-Length", fileinfo.Length.ToString()); Response.ContentType = type; Response.WriteFile(path); } }