|
| 1 | +package synapticloop.b2; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright (c) 2016 Synapticloop. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * This code may contain contributions from other parties which, where |
| 11 | + * applicable, will be listed in the default build file for the project |
| 12 | + * ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. |
| 13 | + * |
| 14 | + * This source code and any derived binaries are covered by the terms and |
| 15 | + * conditions of the Licence agreement ("the Licence"). You may not use this |
| 16 | + * source code or any derived binaries except in compliance with the Licence. |
| 17 | + * A copy of the Licence is available in the file named LICENSE.txt shipped with |
| 18 | + * this source code or binaries. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * <p>Lifecycle rules instruct the B2 service to automatically hide and/or delete |
| 23 | + * old files. You can set up rules to do things like delete old versions of |
| 24 | + * files 30 days after a newer version was uploaded.</p> |
| 25 | + * |
| 26 | + * <p>A bucket can have up to 100 lifecycle rules. Each rule has a fileNamePrefix |
| 27 | + * that specifies which files in the bucket it applies to. Any file whose name |
| 28 | + * starts with the prefix is subject to the rule. A prefix of the empty string, |
| 29 | + * "", means that the rule applies to all files in the bucket.</p> |
| 30 | + * |
| 31 | + * <p>You're not allowed to create two rules that both apply to the same files. |
| 32 | + * For example, a rule with a file name prefix of photos/ and a rule with a file |
| 33 | + * name prefix of photos/kittens/ would both apply to a file named |
| 34 | + * photos/kittens/fluffy.jpg, so you're not allowed to have both rules at the |
| 35 | + * same time.</p> |
| 36 | + * |
| 37 | + * <p>Each lifecycle rule specifies two things:</p> |
| 38 | + * |
| 39 | + * <ul> |
| 40 | + * <li>daysFromUploadingToHiding and</li> |
| 41 | + * <li>daysFromHidingToDeleting.</li> |
| 42 | + * </ul> |
| 43 | + * |
| 44 | + * <p>Either can be null, which means that part of the rule doesn't apply. Setting |
| 45 | + * both to null is not allowed, because the rule would do nothing.</p> |
| 46 | + * |
| 47 | + * <p>Setting daysFromUploadingToHiding to 0 is not allowed. When set, it must be a |
| 48 | + * positive number.</p> |
| 49 | + * |
| 50 | + * <p>The most commonly used setting is daysFromHidingToDeleting, which says how long |
| 51 | + * to keep file versions that are not the current version. A file version counts |
| 52 | + * as hidden when explicitly hidden with b2_hide_file, or when a newer file with |
| 53 | + * the same name is uploaded. When a rule with this setting applies, the file will |
| 54 | + * be deleted the given number of days after it is hidden.</p> |
| 55 | + * |
| 56 | + * <p>For example, if you are backing up your files to B2 using the B2 command-line |
| 57 | + * tool, or another software package that uploads files when they change, B2 will |
| 58 | + * keep old versions of the file. This is very helpful, because it means the old |
| 59 | + * versions are there if the original file is accidentally deleted. On the other |
| 60 | + * hand, keeping them forever can clutter things up. For an application like this, |
| 61 | + * you might want a lifecycle rule that keeps old versions in the backup/ folder |
| 62 | + * for 30 days, and then deletes them:</p> |
| 63 | + * |
| 64 | + * <pre> |
| 65 | + { |
| 66 | + "daysFromHidingToDeleting": 30, |
| 67 | + "daysFromUploadingToHiding": null, |
| 68 | + "fileNamePrefix": "backup/" |
| 69 | + } |
| 70 | + * </pre> |
| 71 | + * |
| 72 | + * <p>The daysFromUploadingToHiding setting is less frequently used. It causes files to |
| 73 | + * be hidden automatically after the given number of days. This can be useful for things |
| 74 | + * like server log files that can be deleted after a while. This rule will keep files |
| 75 | + * in the logs/ folder for 7 days, and then hide and immediately delete them:</p> |
| 76 | + * |
| 77 | + * <pre> |
| 78 | + { |
| 79 | + "daysFromHidingToDeleting": 0, |
| 80 | + "daysFromUploadingToHiding": 7, |
| 81 | + "fileNamePrefix": "logs/" |
| 82 | + } |
| 83 | + * </pre> |
| 84 | + * |
| 85 | + * <p>The API calls related to lifecycle rules are:</p> |
| 86 | + * |
| 87 | + * <ul> |
| 88 | + * <li>b2_create_bucket - creates a new bucket</li> |
| 89 | + * <li>b2_update_bucket - changes the settings on a bucket</li> |
| 90 | + * </ul> |
| 91 | + * |
| 92 | + * <p>When you create a new bucket, you can specify the lifecycle rules. Later, |
| 93 | + * you can change the rules on a bucket by updating it.</p> |
| 94 | + * |
| 95 | + * @author synapticloop |
| 96 | + * |
| 97 | + */ |
| 98 | +public class LifecycleRule { |
| 99 | + private final Long daysFromHidingToDeleting; |
| 100 | + private final Long daysFromUploadingToHiding; |
| 101 | + private final String fileNamePrefix; |
| 102 | + |
| 103 | + public LifecycleRule(Long daysFromHidingToDeleting, Long daysFromUploadingToHiding, String fileNamePrefix) { |
| 104 | + this.daysFromHidingToDeleting = daysFromHidingToDeleting; |
| 105 | + this.daysFromUploadingToHiding = daysFromUploadingToHiding; |
| 106 | + this.fileNamePrefix = fileNamePrefix; |
| 107 | + } |
| 108 | + |
| 109 | + public LifecycleRule(JSONObject jsonObject) { |
| 110 | + long hide = jsonObject.optLong("daysFromHidingToDeleting", Long.MIN_VALUE); |
| 111 | + if(hide == Long.MIN_VALUE) { |
| 112 | + daysFromHidingToDeleting = null; |
| 113 | + } else { |
| 114 | + daysFromHidingToDeleting = hide; |
| 115 | + } |
| 116 | + |
| 117 | + long upload = jsonObject.optLong("daysFromUploadingToHiding", Long.MIN_VALUE); |
| 118 | + if(upload == Long.MIN_VALUE) { |
| 119 | + daysFromUploadingToHiding = null; |
| 120 | + } else { |
| 121 | + daysFromUploadingToHiding = upload; |
| 122 | + } |
| 123 | + |
| 124 | + fileNamePrefix = jsonObject.optString("fileNamePrefix", null); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * The number of days from when the file was hidden to when it will be deleted |
| 129 | + * |
| 130 | + * @return the number of days from hiding to deleting |
| 131 | + */ |
| 132 | + public Long getDaysFromHidingToDeleting() { return this.daysFromHidingToDeleting; } |
| 133 | + |
| 134 | + /** |
| 135 | + * The number of days from when the file was uploaded to when it will be deleted |
| 136 | + * |
| 137 | + * @return the number of days from uploading to deleting |
| 138 | + */ |
| 139 | + public Long getDaysFromUploadingToHiding() { return this.daysFromUploadingToHiding; } |
| 140 | + |
| 141 | + /** |
| 142 | + * The file name prefix to which this rule applies |
| 143 | + * |
| 144 | + * @return the file name prefix to which this rule applies |
| 145 | + */ |
| 146 | + public String getFileNamePrefix() { return this.fileNamePrefix; } |
| 147 | + |
| 148 | +} |
0 commit comments