/// <summary> /// Copy the plugin file to shadow copy directory /// </summary> /// <param name="pluginFilePath">Plugin file path</param> /// <param name="shadowCopyPlugFolder">Path to shadow copy folder</param> /// <returns>File path to shadow copy of plugin file</returns> private static string ShadowCopyFile(string pluginFilePath, string shadowCopyPlugFolder) { var shouldCopy = true; var shadowCopiedPlug = _fileProvider.Combine(shadowCopyPlugFolder, _fileProvider.GetFileName(pluginFilePath)); //check if a shadow copied file already exists and if it does, check if it's updated, if not don't copy if (_fileProvider.FileExists(shadowCopiedPlug)) { //it's better to use LastWriteTimeUTC, but not all file systems have this property //maybe it is better to compare file hash? var areFilesIdentical = _fileProvider.GetCreationTime(shadowCopiedPlug).ToUniversalTime().Ticks >= _fileProvider.GetCreationTime(pluginFilePath).ToUniversalTime().Ticks; if (areFilesIdentical) { Debug.WriteLine("Not copying; files appear identical: '{0}'", _fileProvider.GetFileName(shadowCopiedPlug)); shouldCopy = false; } else { //delete an existing file //More info: https://www.nopcommerce.com/boards/t/11511/access-error-nopplugindiscountrulesbillingcountrydll.aspx?p=4#60838 Debug.WriteLine("New plugin found; Deleting the old file: '{0}'", _fileProvider.GetFileName(shadowCopiedPlug)); _fileProvider.DeleteFile(shadowCopiedPlug); } } if (!shouldCopy) { return(shadowCopiedPlug); } try { _fileProvider.FileCopy(pluginFilePath, shadowCopiedPlug, true); } catch (IOException) { Debug.WriteLine(shadowCopiedPlug + " is locked, attempting to rename"); //this occurs when the files are locked, //for some reason devenv locks plugin files some times and for another crazy reason you are allowed to rename them //which releases the lock, so that it what we are doing here, once it's renamed, we can re-shadow copy try { var oldFile = shadowCopiedPlug + Guid.NewGuid().ToString("N") + ".old"; _fileProvider.FileMove(shadowCopiedPlug, oldFile); } catch (IOException exc) { throw new IOException(shadowCopiedPlug + " rename failed, cannot initialize plugin", exc); } //OK, we've made it this far, now retry the shadow copy _fileProvider.FileCopy(pluginFilePath, shadowCopiedPlug, true); } return(shadowCopiedPlug); }
/// <summary> /// Luu work file /// </summary> /// <param name="item"></param> /// <param name="fileContent"></param> void SaveWorkFileOnDisk(WorkFile item, byte[] fileContent) { string _pathStore = item.CreatedDate.ToPathFolderStore(); _pathStore = _fileProvider.Combine(_fileProvider.MapPath(GSDataSettingsDefaults.FolderWorkFiles), _pathStore); _fileProvider.CreateDirectory(_pathStore); var _fileStore = _fileProvider.Combine(_pathStore, item.FileGuid.ToString() + item.Extension); _fileProvider.WriteAllBytes(_fileStore, fileContent); }
/// <summary> /// Сopy the directory with the embedded files and directories /// </summary> /// <param name="sourcePath">Path to the source directory</param> /// <param name="destinationPath">Path to the destination directory</param> protected virtual void CopyDirectory(string sourcePath, string destinationPath) { var existingFiles = _fileProvider.GetFiles(sourcePath); var existingDirectories = _fileProvider.GetDirectories(sourcePath); if (!_fileProvider.DirectoryExists(destinationPath)) { _fileProvider.CreateDirectory(destinationPath); } foreach (var file in existingFiles) { var filePath = _fileProvider.Combine(destinationPath, _fileProvider.GetFileName(file)); if (!_fileProvider.FileExists(filePath)) { _fileProvider.FileCopy(file, filePath); } } foreach (var directory in existingDirectories) { var directoryPath = _fileProvider.Combine(destinationPath, _fileProvider.GetDirectoryName(directory)); CopyDirectory(directory, directoryPath); } }
public virtual IActionResult Upload() { if (!_permissionService.Authorize(StandardPermissionProvider.HtmlEditorManagePictures)) { ViewData["resultCode"] = "failed"; ViewData["result"] = "No access to this functionality"; return(View()); } if (Request.Form.Files.Count == 0) { throw new Exception("No file uploaded"); } var uploadFile = Request.Form.Files.FirstOrDefault(); if (uploadFile == null) { ViewData["resultCode"] = "failed"; ViewData["result"] = "No file name provided"; return(View()); } var fileName = _fileProvider.GetFileName(uploadFile.FileName); if (string.IsNullOrEmpty(fileName)) { ViewData["resultCode"] = "failed"; ViewData["result"] = "No file name provided"; return(View()); } var directory = "~/wwwroot/images/uploaded/"; var filePath = _fileProvider.Combine(_fileProvider.MapPath(directory), fileName); var fileExtension = _fileProvider.GetFileExtension(filePath); if (!GetAllowedFileTypes().Contains(fileExtension)) { ViewData["resultCode"] = "failed"; ViewData["result"] = $"Files with {fileExtension} extension cannot be uploaded"; return(View()); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { uploadFile.CopyTo(fileStream); } ViewData["resultCode"] = "success"; ViewData["result"] = "success"; ViewData["filename"] = Url.Content($"{directory}{fileName}"); return(View()); }
/// <summary> /// Get font /// </summary> /// <param name="fontFileName">Font file name</param> /// <returns>Font</returns> protected virtual Font GetFont(string fontFileName) { if (fontFileName == null) { throw new ArgumentNullException(nameof(fontFileName)); } var fontPath = _fileProvider.Combine(_fileProvider.MapPath("~/App_Data/Pdf/"), fontFileName); var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); var font = new Font(baseFont, 10, Font.NORMAL); return(font); }
/// <summary> /// Prepare the favicon model /// </summary> /// <returns>Favicon model</returns> public virtual FaviconModel PrepareFaviconModel() { var model = new FaviconModel(); //try loading a store specific favicon var faviconFileName = $"favicon-{_storeContext.CurrentStore.Id}.ico"; var localFaviconPath = _fileProvider.Combine(_hostingEnvironment.WebRootPath, faviconFileName); if (!_fileProvider.FileExists(localFaviconPath)) { //try loading a generic favicon faviconFileName = "favicon.ico"; localFaviconPath = _fileProvider.Combine(_hostingEnvironment.WebRootPath, faviconFileName); if (!_fileProvider.FileExists(localFaviconPath)) { return(model); } } model.FaviconUrl = _webHelper.GetStoreLocation() + faviconFileName; return(model); }
public virtual IActionResult MaintenanceDeleteFiles(MaintenanceModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenance)) { return(AccessDeniedView()); } var startDateValue = model.DeleteExportedFiles.StartDate == null ? null : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.DeleteExportedFiles.StartDate.Value, _dateTimeHelper.CurrentTimeZone); var endDateValue = model.DeleteExportedFiles.EndDate == null ? null : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.DeleteExportedFiles.EndDate.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1); model.DeleteExportedFiles.NumberOfDeletedFiles = 0; foreach (var fullPath in _fileProvider.GetFiles(_fileProvider.GetAbsolutePath(EXPORT_IMPORT_PATH))) { try { var fileName = _fileProvider.GetFileName(fullPath); if (fileName.Equals("index.htm", StringComparison.InvariantCultureIgnoreCase)) { continue; } var info = _fileProvider.GetFileInfo(_fileProvider.Combine(EXPORT_IMPORT_PATH, fileName)); var lastModifiedTimeUtc = info.LastModified.UtcDateTime; if ((!startDateValue.HasValue || startDateValue.Value < lastModifiedTimeUtc) && (!endDateValue.HasValue || lastModifiedTimeUtc < endDateValue.Value)) { _fileProvider.DeleteFile(fullPath); model.DeleteExportedFiles.NumberOfDeletedFiles++; } } catch (Exception exc) { ErrorNotification(exc, false); } } return(View(model)); }
/// <summary> /// Get picture (thumb) local path /// </summary> /// <param name="thumbFileName">Filename</param> /// <returns>Local picture thumb path</returns> protected virtual string GetThumbLocalPath(string thumbFileName) { var thumbsDirectoryPath = _fileProvider.GetAbsolutePath(GSMediaDefaults.ImageThumbsPath); if (_mediaSettings.MultipleThumbDirectories) { //get the first two letters of the file name var fileNameWithoutExtension = _fileProvider.GetFileNameWithoutExtension(thumbFileName); if (fileNameWithoutExtension != null && fileNameWithoutExtension.Length > GSMediaDefaults.MultipleThumbDirectoriesLength) { var subDirectoryName = fileNameWithoutExtension.Substring(0, GSMediaDefaults.MultipleThumbDirectoriesLength); thumbsDirectoryPath = _fileProvider.GetAbsolutePath(GSMediaDefaults.ImageThumbsPath, subDirectoryName); _fileProvider.CreateDirectory(thumbsDirectoryPath); } } var thumbFilePath = _fileProvider.Combine(thumbsDirectoryPath, thumbFileName); return(thumbFilePath); }
static PluginManager() { //we use the default file provider, since the DI isn't initialized yet _fileProvider = CommonHelper.DefaultFileProvider; //get all libraries from /bin/{version}/ directory _baseAppLibraries = _fileProvider.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll").Select(fi => _fileProvider.GetFileName(fi)).ToList(); //get all libraries from base site directory if (!AppDomain.CurrentDomain.BaseDirectory.Equals(Environment.CurrentDirectory, StringComparison.InvariantCultureIgnoreCase)) { _baseAppLibraries.AddRange(_fileProvider.GetFiles(Environment.CurrentDirectory, "*.dll").Select(fi => _fileProvider.GetFileName(fi))); } //get all libraries from refs directory var refsPathName = _fileProvider.Combine(Environment.CurrentDirectory, GSPluginDefaults.RefsPathName); if (_fileProvider.DirectoryExists(refsPathName)) { _baseAppLibraries.AddRange(_fileProvider.GetFiles(refsPathName, "*.dll").Select(fi => _fileProvider.GetFileName(fi))); } }
/// <summary> /// Returns the path to the backup file /// </summary> /// <param name="backupFileName">The name of the backup file</param> /// <returns>The path to the backup file</returns> public virtual string GetBackupPath(string backupFileName) { return(_fileProvider.Combine(GetBackupDirectoryPath(), backupFileName)); }