-
Notifications
You must be signed in to change notification settings - Fork 370
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
feat: add file.isPublic() function #708
Changes from 5 commits
0a51cc0
ad17498
c33a08e
49949d0
769cc2a
73381ff
819bbc7
2c6bcfb
4373b0e
d0b5dae
2298e67
2b2b000
df42ac9
f577305
06474dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ import { | |
} from '@google-cloud/common/build/src/util'; | ||
const duplexify: DuplexifyConstructor = require('duplexify'); | ||
import {normalize, objectEntries} from './util'; | ||
import {Headers} from 'gaxios'; | ||
import {GaxiosError, Headers, request as gaxiosRequest} from 'gaxios'; | ||
|
||
export type GetExpirationDateResponse = [Date]; | ||
export interface GetExpirationDateCallback { | ||
|
@@ -211,6 +211,12 @@ export type MakeFilePrivateResponse = [Metadata]; | |
|
||
export interface MakeFilePrivateCallback extends SetFileMetadataCallback {} | ||
|
||
export interface IsPublicCallback { | ||
(err: Error | null, resp?: boolean): void; | ||
} | ||
|
||
export type IsPublicResponse = [boolean]; | ||
|
||
export type MakeFilePublicResponse = [Metadata]; | ||
|
||
export interface MakeFilePublicCallback { | ||
|
@@ -2595,6 +2601,63 @@ class File extends ServiceObject<File> { | |
}); | ||
} | ||
|
||
isPublic(): Promise<IsPublicResponse>; | ||
isPublic(callback: IsPublicCallback): void; | ||
/** | ||
* @callback IsPublicCallback | ||
* @param {?Error} err Request error, if any. | ||
* @param {boolean} resp Whether file is public or not. | ||
*/ | ||
/** | ||
* @typedef {array} IsPublicResponse | ||
* @property {boolean} 0 Whether file is public or not. | ||
*/ | ||
/** | ||
* Check whether this file is public or not. | ||
* @param {IsPublicCallback} [callback] Callback function. | ||
* @returns {Promise<IsPublicResponse>} | ||
* | ||
* @example | ||
* const {Storage} = require('@google-cloud/storage'); | ||
* const storage = new Storage(); | ||
* const myBucket = storage.bucket('my-bucket'); | ||
* | ||
* const file = myBucket.file('my-file'); | ||
* | ||
* //- | ||
* // Check whether the file is publicly accessible. | ||
* //- | ||
* file.isPublic(function(err, resp) { | ||
* if (err) { | ||
* console.error(err); | ||
* return; | ||
* } | ||
* console.log(`the file ${file.id} is public: ${resp}`) ; | ||
* }) | ||
* //- | ||
* // If the callback is omitted, we'll return a Promise. | ||
* //- | ||
* file.isPublic().then(function(data) { | ||
* const resp = data[0]; | ||
* }); | ||
*/ | ||
|
||
isPublic(callback?: IsPublicCallback): Promise<IsPublicResponse> | void { | ||
gaxiosRequest({ | ||
method: 'HEAD', | ||
stephenplusplus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url: `http://${this.bucket.name}.storage.googleapis.com/${this.id}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to encode the name: url: `http://${this.bucket.name}.storage.googleapis.com/${encodeURIComponent(this.name)}`, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, done It only matters in the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you mean the
I suppose. |
||
}).then( | ||
() => callback!(null, true), | ||
(err: GaxiosError) => { | ||
if (err.code === '403') { | ||
callback!(null, false); | ||
} else { | ||
callback!(err); | ||
} | ||
} | ||
); | ||
} | ||
|
||
makePrivate( | ||
options?: MakeFilePrivateOptions | ||
): Promise<MakeFilePrivateResponse>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should explain how we are doing this. It is possible the server has a hiccup and returns an error code, even though the file is public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Thanks