www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

storageUtilities.js (3340B)


      1 Zotero.Sync.Storage.Utilities = {
      2 	getClassForMode: function (mode) {
      3 		switch (mode) {
      4 		case 'zfs':
      5 			return Zotero.Sync.Storage.Mode.ZFS;
      6 		
      7 		case 'webdav':
      8 			return Zotero.Sync.Storage.Mode.WebDAV;
      9 		
     10 		default:
     11 			throw new Error("Invalid storage mode '" + mode + "'");
     12 		}
     13 	},
     14 	
     15 	getItemFromRequest: function (request) {
     16 		var [libraryID, key] = request.name.split('/');
     17 		return Zotero.Items.getByLibraryAndKey(libraryID, key);
     18 	},
     19 	
     20 	
     21 	/**
     22 	 * Create zip file of attachment directory in the temp directory
     23 	 *
     24 	 * @param	{Zotero.Sync.Storage.Request}		request
     25 	 * @return {Promise<Boolean>} - True if the zip file was created, false otherwise
     26 	 */
     27 	createUploadFile: Zotero.Promise.coroutine(function* (request) {
     28 		var item = this.getItemFromRequest(request);
     29 		Zotero.debug("Creating ZIP file for item " + item.libraryKey);
     30 		
     31 		switch (item.attachmentLinkMode) {
     32 			case Zotero.Attachments.LINK_MODE_LINKED_FILE:
     33 			case Zotero.Attachments.LINK_MODE_LINKED_URL:
     34 				throw new Error("Upload file must be an imported snapshot or file");
     35 		}
     36 		
     37 		var zipFile = OS.Path.join(Zotero.getTempDirectory().path, item.key + '.zip');
     38 		
     39 		return Zotero.File.zipDirectory(
     40 			Zotero.Attachments.getStorageDirectory(item).path,
     41 			zipFile,
     42 			{
     43 				onStopRequest: function (req, context, status) {
     44 					var zipFileName = OS.Path.basename(zipFile);
     45 					
     46 					var originalSize = 0;
     47 					for (let entry of context.entries) {
     48 						let zipEntry = context.zipWriter.getEntry(entry.name);
     49 						if (!zipEntry) {
     50 							Zotero.logError("ZIP entry '" + entry.name + "' not found for "
     51 								+ "request '" + request.name + "'")
     52 							continue;
     53 						}
     54 						originalSize += zipEntry.realSize;
     55 					}
     56 					
     57 					Zotero.debug("Zip of " + zipFileName + " finished with status " + status
     58 						+ " (original " + Math.round(originalSize / 1024) + "KB, "
     59 						+ "compressed " + Math.round(context.zipWriter.file.fileSize / 1024) + "KB, "
     60 						+ Math.round(
     61 							((originalSize - context.zipWriter.file.fileSize) / originalSize) * 100
     62 						) + "% reduction)");
     63 				}
     64 			}
     65 		);
     66 	}),
     67 	
     68 	
     69 	/**
     70 	 * Prompt whether to reset unsynced local files in a library
     71 	 *
     72 	 * Keep in sync with Sync.Data.Utilities.showWriteAccessLostPrompt()
     73 	 *
     74 	 * @param {Window|null} win
     75 	 * @param {Zotero.Library} library
     76 	 * @return {Integer} - 0 to reset, 1 to skip
     77 	 */
     78 	showFileWriteAccessLostPrompt: function (win, library) {
     79 		var libraryType = library.libraryType;
     80 		switch (libraryType) {
     81 		case 'group':
     82 			var msg = Zotero.getString('sync.error.groupFileWriteAccessLost',
     83 					[library.name, ZOTERO_CONFIG.DOMAIN_NAME])
     84 				+ "\n\n"
     85 				+ Zotero.getString('sync.error.groupCopyChangedFiles')
     86 			var button1Text = Zotero.getString('sync.resetGroupFilesAndSync');
     87 			var button2Text = Zotero.getString('sync.skipGroup');
     88 			break;
     89 		
     90 		default:
     91 			throw new Error("Unsupported library type " + libraryType);
     92 		}
     93 		
     94 		var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
     95 			.getService(Components.interfaces.nsIPromptService);
     96 		var buttonFlags = (ps.BUTTON_POS_0) * (ps.BUTTON_TITLE_IS_STRING)
     97 			+ (ps.BUTTON_POS_1) * (ps.BUTTON_TITLE_IS_STRING)
     98 			+ ps.BUTTON_DELAY_ENABLE;
     99 		
    100 		return ps.confirmEx(
    101 			win,
    102 			Zotero.getString('general.permissionDenied'),
    103 			msg,
    104 			buttonFlags,
    105 			button1Text,
    106 			button2Text,
    107 			null,
    108 			null, {}
    109 		);
    110 	}
    111 }